What is the psutil.boot_time() method in Python?

What is the psutil module?

The psutil (Python system and process utilities) module is a Python package that retrieves information on ongoing processes and system usage (CPU, memory, storage, network, and sensors). It is mostly used for system monitoring, profiling, restricting process resources, and process management.

This module can be installed via pip as follows:

pip install psutil

The boot_time method

The boot_time method is used to return the system boot time, expressed in seconds since the epoch.

Boot time is the time taken by the system to be operational once the power is switched on.

Method signature

Let’s look at the signature for this method:

psutil.boot_time()

Parameter values

This method takes no parameters.

Example

Let’s look at a code example of this method:

import psutil, datetime
print("The system boot time is: ")
bt = psutil.boot_time()
print(bt, end="")
print(" seconds")
print("Formatting time: ")
print(datetime.datetime.fromtimestamp(bt).strftime("%Y-%m-%d %H:%M:%S"))

Explanation

  • Line 1: We import the psutil and datetime modules.
  • Line 4: We retrieve the system boot time using the boot_time() method, and store it in the variable called bt.
  • Lines 5–6: We print the bt variable.
  • Lines 8–9: We use the fromtimestamp method from the datetime module to format bt into a human-readable data format.

Free Resources