Fastest way to get system uptime in Python in Linux

后端 未结 5 875
無奈伤痛
無奈伤痛 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 11:00

    You can try installing psutil with:

    pip install psutil
    

    and then use the following fragment of code:

    import psutil
    import time
    
    
    def seconds_elapsed():
        return time.time() - psutil.boot_time()
    
    
    print seconds_elapsed()
    

提交回复
热议问题