A Simple Guide to Python Get Disk or Directory Total Space, Used Space and Free Space – Python Tutorial

By | September 4, 2019

In this tutorial, we will introduce how to get disk or directory total space, used space and free space using python, which is very helpful if you want to save some files on you computer.

Import library

import shutil

Get total, used and free space information


The disk usage information is:

usage(total=128857235456, used=113601175552, free=15256059904)

As to shutil.disk_usage() function.


On windows, path must be a directory; on unix, it can be a file or directory.

Meanwhile, the disk usage information is not user friendly, we can format it.

Format disk usage

def formatSize(bytes):
        bytes = float(bytes)
        kb = bytes / 1024
        return "Error"

    if kb >= 1024:
        M = kb / 1024
        if M >= 1024:
            G = M / 1024
            return "%.2fG" % (G)
            return "%.2fM" % (M)
        return "%.2fkb" % (kb)

As to free space, we can get it like this:

usage = shutil.disk_usage("F:\\")
free_space = formatSize(usage[2])

The free space is: 14.21G

Of course, you also can use other way to get disk free space, you can read this tutorial.

Best Practice to Python Calculate Disk Free Space for Beginners – Python Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *