Python Script to check PC last reboot time

psutil is a cross-platform library for retrieving information on running processes and system utilization(CPU, memory, disks, networks, sensors) in Python. The Python script below can be run in both Windows and Linux. psutil library can be installed using the terminal by:

In Windows:

pip install psutil

In Linux:

sudo apt-get install gcc python3-dev
sudo pip3 install psutil

Code:

Python3



filter_none

edit
close

play_arrow

link
brightness_4
code

import psutil
import datetime
  
# returns the time in seconds since the epoch
last_reboot = psutil.boot_time()
  
# coverting the date and time in readable format
print(datetime.datetime.fromtimestamp(last_reboot))

chevron_right


Output:

2020-08-20 16:46:48

Explanation: 

psutil.boot_time() returns the system boot time expressed in seconds since the epoch. It is the number of seconds that have elapsed since the Unix epoch, minus leap seconds; the Unix epoch is 00:00:00 UTC on 1 January 1970. 
To convert this time to DateTime we use fromtimestamp(), which returns the local date and time.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

5


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.