get system uptime in python

To get the system uptime in python, we can use the subprocess module to run the shell command to get the uptime information. Here's the code:

main.py
import subprocess

# run the command to get the uptime information
output = subprocess.check_output(['uptime'])

# decode the byte string to utf-8
uptime_info = output.decode('utf-8').strip()

# print the uptime information
print(uptime_info)
243 chars
11 lines

This will print the system uptime information in the format:

main.py
18:52 up 3 days ...
20 chars
2 lines

Note that the format of the uptime information may differ depending on the operating system.

gistlibby LogSnag