Python Script: Check the Free Disk Space (Source code download)


It is always important to know how much space is left on your hard drive. This is especially true when you are working with large files or directories. Python makes it easy to check the amount of free space on your hard drive. In this blog post, we will show you how to check the free disk space in Python.

Check the Free Disk Space

There are a few different ways to check the free disk space in Python. The easiest way is to use the os module. The os module has a function called disk_usage. This function takes a path argument and returns an object containing information about the free space on the specified path. We can use this function to check the free disk space on our hard drive like this:

import os disk_usage = os.disk_usage("/") print(disk_usage)
Code language: JavaScript (javascript)

Another way to check the free disk space in Python is to use the shutil module. The shutil module has a function called disk_usage. This function takes a path argument and returns an object containing information about the free space on the specified path. We can use this function to check the free disk space on our hard drive like this:

import shutil disk_usage = shutil.disk_usage("/") print(disk_usage)
Code language: JavaScript (javascript)

Full Python Script

import shutil import argparse import datetime import time def get_free_space(path): """ Return the free space of the given path """ _, _, free = shutil.disk_usage(path) return free def get_free_space_hr(path): """ Return the free space of the given path in a human readable format """ free = get_free_space(path) hr_free = '' for unit, idx in zip(['k', 'M', 'G', 'T', 'P'], range(10, 50, 10)): hr_usage = (free % (2 ** (idx + 10))) // (2 ** idx) if hr_usage == 0: break hr_free = f"{hr_usage}{unit} {hr_free}" return hr_free if __name__ == '__main__': parser = argparse.ArgumentParser( description='Display and log the free disk space from a given folder') parser.add_argument('path', help="Path for using the disk usage", type=str) parser.add_argument('-n', '--interval', help="Interval in seconds (default: 2)", type=int, default=2) parser.add_argument('-f', '--logfile', help="Path to logfile to log the disk usage", type=str, default=None) parser.add_argument('-q', '--quite', help="Quite mode: No output will be displayed", action="store_true") args = parser.parse_args() while True: hr_free = get_free_space_hr(args.path) now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") if not args.quite: print(f"{now} {hr_free}") if args.logfile: with open(args.logfile, 'a') as outfile: outfile.write(f"{now} {hr_free}\n") time.sleep(args.interval)
Code language: Python (python)

Display and/log the free space of a given path every N seconds.

<strong>usage: </strong>disk_usage.py [-h] [-n INTERVAL] [-f LOGFILE] [-q] path
Code language: HTML, XML (xml)

Display and log the free disk space from a given folder

positional arguments: path Path for using the disk usage

optional arguments: -h, –help show this help message and exit -n INTERVAL, –interval INTERVAL Interval in seconds (default: 2) -f LOGFILE, –logfile LOGFILE Path to logfile to log the disk usage -q, –quite Quite mode: No output will be displayed

Download the script here

In conclusion, there are a few different ways to check the free disk space in Python. The easiest way is to use the os module. Another way is to use the shutil module. Whichever method you choose, be sure to import the appropriate module before using the function. Thanks for reading!

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts