Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator
psutil

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

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The psutil module

psutil (Python system and process utilities) 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.

The module can be installed via pip as follows:

pip install psutil

disk_usage method

The sensors_fans method returns the disk usage statistics about the partition of the given path as a named tuple. The named tuple consists of the following attributes:

  • total: This represents the total amount of memory.
  • used: This represents used memory.
  • free: This represents free memory.
  • percent: This represents the percentage usage of the memory.

All the above attributes are reported in bytes.

Syntax

psutil.disk_usage(path)
  • path: This is the file path.

Parameter

The method has no parameters.

Code

import psutil, os
print("The usage statistics of " + os.getcwd() + " is:")
print(psutil.disk_usage(os.getcwd()))
Implementation of psutil.disk_usage()

Explanation

  • Line 1: We import the psutil and os modules.
  • Line 4: We use the psutil.disk_usage() method to retrieve the usage statistics of the current working directory, and pass the current working directory as the file path. We use the os.getcwd() method to obtain the current working directory.

RELATED TAGS

python
communitycreator
psutil

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring