py可以获取explorer启动时间吗?我想使用这个计算系统的开机时间。如果没有的话有没有其他替代方案
(直接使用启动项+获取当前时间-开机时间这种方法并不准确,有没有既准确又便于操作的方法?)
python如何获取开机时间?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
关注 让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
可以使用Win32 API获取Explorer进程的创建时间来间接获取Explorer启动时间,再通过系统的开机时间计算出系统的实际开机时间。具体实现如下:import psutil import datetime def get_explorer_start_time(): for proc in psutil.process_iter(): if proc.name() == "explorer.exe": create_time = datetime.datetime.fromtimestamp(proc.create_time()) return create_time return None def get_system_boot_time(): boot_time_timestamp = psutil.boot_time() boot_time = datetime.datetime.fromtimestamp(boot_time_timestamp) return boot_time # 计算系统实际开机时间 def get_system_actual_boot_time(): explorer_start_time = get_explorer_start_time() system_boot_time = get_system_boot_time() if explorer_start_time is None: return system_boot_time else: elapsed_time = explorer_start_time - system_boot_time actual_boot_time = datetime.datetime.now() - elapsed_time return actual_boot_time
这里使用了psutil库获取系统进程信息,并使用datetime库进行时间的计算和转换。其中get_explorer_start_time()函数用于获取Explorer进程的创建时间,get_system_boot_time()函数用于获取系统的开机时间,而get_system_actual_boot_time()函数则是最终计算出系统实际的开机时间并返回。 示例代码在Windows 10系统下测试通过,但在其他操作系统下可能需要进行适当的修改。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥20 wpf datagrid单元闪烁效果失灵
- ¥15 券商软件上市公司信息获取问题
- ¥100 ensp启动设备蓝屏,代码clock_watchdog_timeout
- ¥15 Android studio AVD启动不了
- ¥15 陆空双模式无人机怎么做
- ¥15 想咨询点问题,与算法转换,负荷预测,数字孪生有关
- ¥15 C#中的编译平台的区别影响
- ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
- ¥15 电脑蓝屏logfilessrtsrttrail问题
- ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)