Home >Backend Development >Python Tutorial >How to check disk free space in Python

How to check disk free space in Python

WBOY
WBOYforward
2024-03-02 13:10:02999browse

How to check disk free space in Python

To check the free space of the disk, you can use the shutil module of python to get the total space and free space of the disk. The following is a simple sample code:

import shutil

total, used, free = shutil.disk_usage("/")

print("Total disk space:", total // (2**30), "GB")
print("Used disk space:", used // (2**30), "GB")
print("Free disk space:", free // (2**30), "GB")

In the above code, the shutil.disk_usage() function accepts a path as a parameter and returns a tuple containing the total space, used space and free space. We divide these values ​​by 2**30 to convert bytes to GB units.

Please note that the path here "/" represents the root directory. You can change the path as needed to check the available space on other disks or directories.

The above is the detailed content of How to check disk free space in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lsjlt.com. If there is any infringement, please contact admin@php.cn delete