Fastest way to get system uptime in Python in Linux

后端 未结 5 811
無奈伤痛
無奈伤痛 2021-01-11 10:24

I\'m looking for a fast and lightweight way to read system uptime from a Python script. Is there a way to call the sysinfo Linux system call from Python?

<
5条回答
  •  情话喂你
    2021-01-11 10:54

    This frankly seems like a much better solution:

    def get_uptime():
        with open('/proc/uptime', 'r') as f:
            uptime_seconds = float(f.readline().split()[0])
    
        return uptime_seconds
    

    It also has the added benefit of not requiring any additional modules.

    Credits: Source

提交回复
热议问题