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

How to check disk free space in Python

WBOY
Release: 2024-03-02 13:10:02
forward
1031 people have browsed it

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")
Copy after login

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!

Related labels:
source:lsjlt.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template