Python Program to Generate Disk Usage Report in CSV File
Last Updated :
05 Sep, 2024
In this article, we will see how we could generate disk usage reports and store them in a CSV file using Python using multiple methods.
Required Modules
We will be using 4 methods here, the modules required would be Shutil, pandas, WMI, OS, and subprocess.
- WMI – WMI is a set of specifications from Microsoft for consolidating the management of devices and applications in a network from Windows computing systems. WMI provides users with data regarding the condition of native or remote pc systems.Â
- We will use shutil and WMI in different methods, but pandas will be common in both of them, so to install pandas, write the following command in the VScode terminal/ Pycharm terminal or normal Command Prompt.
pip install pandas
Using WMI Module to Generate Disk Usage Report in CSV File
Using the WMI module we can easily fetch the name of the disk, the total size of that disk, and the amount of free space it has (both in bytes). Install the WMI module using the following command:
pip install WMI
Here take a variable called key(user can use any name) to initialize the WMI method of the WMI module using which we will get the usage report, then next 3 lines are just 3 blank lists to store the drive name, free_space and total_size of that drive.
Python
import wmi
import pandas as pd
key = wmi.WMI()
drive_name = []
free_space = []
total_size = []
for drive in key.Win32_LogicalDisk():
drive_name.append(drive.Caption)
free_space.append(round(int(drive.FreeSpace)/1e+9, 2))
total_size.append(round(int(drive.Size)/1e+9, 2))
print("================")
print("Drive Name :", drive.Caption+"\n================",
"\nFree Space Available in GB : \n", round(
int(drive.FreeSpace)/1e+9, 2),
"\nTotal Size in GB :\n", round(int(drive.Size)/1e+9, 2))
size_dict = {'Directory Name': drive_name,
'Free Space (in GB)': free_space,
'Total Size (in GB)': total_size}
data_frame = pd.DataFrame(size_dict)
data_frame.to_csv("disk_usage.csv")
Here we are using an iterable drive and using the key variable we are calling the method Win32_LogicalDisk() (Win32_LogicalDisk WMI class represents a data source that resolves to an actual local storage device on a computer system running Windows).
- drive.Caption – Shows the name of the drive.
- drive.FreeSpace – shows the total freespace available in that drive in bytes (That’s why converted to GB by dividing it by 1e+9 and rounding it to 2 decimal places).
- drive.Size – Shows the total size of that drive in bytes ((That’s why converted to GB by dividing it by 1e+9 and rounding it to 2 decimal places).

Disk usage report using the WMI module
Screenshot of the newly created CSV file:

Â
Using the Shutil Module to Generate Disk Usage Report in CSV File
Using the Shutil module we can easily get the total size, used space, and free space of any path provided as an argument to it. Install the Shutil module using the following command.
pip install pytest-shutil
Python
import shutil
import pandas as pd
# Path
drive_name = "C:\\"
# Get the disk usage statistics
# about the given path
stats = shutil.disk_usage(drive_name)
total_size = round(stats[0]/1e+9, 2)
free_space = round(stats[2]/1e+9, 2)
used_space = round(stats[1]/1e+9, 2)
values = [drive_name, total_size, free_space, used_space]
index = ['Drive Name',
'Total Size (in GB)', 'Free Space (in GB)',
'Used Space (in GB)']
data_frame = pd.DataFrame(list(zip(index, values)),
columns=['Information', 'Report'])
data_frame.to_csv("disk_usage.csv")
# Print disk usage statistics
print("Disk usage statistics:")
print("Total Size : ", total_size,
"Free Space Available : ", free_space,
"Used_Space : ", used_space)
Output:

Disk usage report using the Sutil module
Here, storing the drive_name first, then passing it as an argument of the method disk_usage() of Shutil module, now using 3 variables we are storing total_size, free_space, and used_space, the disk.usage() method returns the output as a tuple and from that tuple using the index values we are fetching the required ones and converting them to GB from bytes and round them to 2 decimal places. Then using the zip function and two lists we are converting them into a DataFrame and storing it into a CSV file, Also printing the values in the terminal.
The newly created CSV file and its contents are shown below:

Â
Here the sizes are in GB, if the user wants they can change it to MB or anything.
Using OS Module to Generate Disk Usage Report in CSV File
Using the OS module we can also find out the size of the directory.
Python
import os
import pandas as pd
def get_size(start_path):
total_size = 0
for d_path, d_names, f_name in os.walk(start_path):
for f in f_name:
fp = os.path.join(d_path, f)
# skip if found as symbolic link
if not os.path.islink(fp):
total_size += os.path.getsize(fp)
return total_size/1e+9
sizes = []
paths = []
# The value of range would be the
# number of drive present in user's device
for i in range(2):
path = input("Please enter the Path : ")
paths.append(path)
# Drive paths must have a colon
# and forward slash after their name
sizes.append(get_size(path+":\\"))
df = pd.DataFrame({
"Drive Name": paths,
"Size of Drive in GB": sizes},
# User can give any thing as the index,
# but the amount of them should be same
# as the number of drives passed
index=[1, 2])
df.to_csv("disk_usage_os.csv")
print(df)
Output:

Disk usage report using the OS module
Data in the CSV file will be as shown below:

Â
Using the Subprocess Module  to Generate Disk Usage Report in CSV File
Using the subprocess module we can generate the disk usage report and save it as CSV, but this can only be possible if we are using Linux (to save locally) or any online environment like Jupyter Notebook or Google Collab.
If a user encounters anything like a subprocess module not found use the following command to install it:
pip install subprocess.run
If the user encounters an error with the pandas too please use the above-mentioned command to install it.
Python
import subprocess
import pandas as pd
process = subprocess.Popen(['df', '-h'],
stdout=subprocess.PIPE)
usage = str(process.communicate()[0]).split("\\n")
data = []
for i in range(len(usage)-1):
data.append(usage[i].split())
index = data[0]
df = pd.DataFrame(data, columns=index)
data = df.loc[:, ~df.columns.isin(['on', 'Mounted'])]
data.to_csv("disk_usage.csv", header=False)
print(data)
Output:

Disk usage report using the subprocess module
Data in the CSV file will be as shown below:

Â
Using the psutil Module to Generate Disk Usage Report in CSV File
An alternate approach for generating a disk usage report in a CSV file using Python is to use the psutil library. This library provides various functions for retrieving system information, including disk usage.
To use the psutil library, you need to first install it using pip install psutil. Then, you can use the disk_usage function to retrieve the total size, used space, and free space of a given path.
Here is an example of how to use the psutil library to generate a disk usage report in a CSV file:
Python
import psutil
import pandas as pd
# Path to the drive
drive_name = "C:\\"
# Get the disk usage statistics
stats = psutil.disk_usage(drive_name)
total_size = stats.total / 1024**3 # Convert bytes to GB
used_space = stats.used / 1024**3
free_space = stats.free / 1024**3
# Create a dictionary with the disk usage data
data = {'Directory Name': drive_name, 'Total Size (in GB)': total_size,
'Used Space (in GB)': used_space, 'Free Space (in GB)': free_space}
import pandas as pd
# Create a Pandas DataFrame with an index
df = pd.DataFrame(data, index=[0])
# Write the DataFrame to a CSV file
df.to_csv("disk_usage.csv", index=False)
# Print disk usage statistics
print("Disk usage statistics:")
print("Total Size : ", total_size,
"Free Space Available : ", free_space,
"Used_Space : ", used_space)
This code will generate a CSV file with the following columns: Directory Name, Total Size (in GB), Used Space (in GB), and Free Space (in GB). The values in these columns will correspond to the drive specified in the drive_name variable.
Output:
Disk usage statistics:
Total Size : 237.22753524780273
Free Space Available 57.553279876708984
Used_Space : 179.67425537109375
Similar Reads
Get File Size in Bytes, Kb, Mb, And Gb using Python
Handling file sizes in Python is a common task, especially when working with data processing, file management, or simply understanding resource usage. Fortunately, Python provides several methods to obtain file sizes in various units, such as bytes, kilobytes, megabytes, and gigabytes. In this artic
2 min read
How to Add Numbers in a Csv File Using Python
When working with CSV files in Python, adding numbers in the CSV file is a common requirement. This article will guide you through the process of adding numbers within a CSV file. Whether you're new to data analysis or an experienced practitioner, understanding this skill is vital for efficient data
3 min read
How To Get The Uncompressed And Compressed File Size Of A File In Python
We are given a compressed file as well as uncompressed file. Our task is to get the size of uncompressed as well as compressed file in Python. In this article, we will see how we can get the uncompressed and compressed file size of a file in Python. What is Uncompressed And Compressed File?Uncompres
3 min read
How to convert tab-separated file into a dataframe using Python
In this article, we will learn how to convert a TSV file into a data frame using Python and the Pandas library. A TSV (Tab-Separated Values) file is a plain text file where data is organized in rows and columns, with each column separated by a tab character. It is a type of delimiter-separated file,
4 min read
How to Convert Tab-Delimited File to Csv in Python?
We are given a tab-delimited file and we need to convert it into a CSV file in Python. In this article, we will see how we can convert tab-delimited files to CSV files in Python. Convert Tab-Delimited Files to CSV in PythonBelow are some of the ways to Convert Tab-Delimited files to CSV in Python: U
2 min read
Python Loop through Folders and Files in Directory
File iteration is a crucial process of working with files in Python. The process of accessing and processing each item in any collection is called File iteration in Python, which involves looping through a folder and perform operation on each file. In this article, we will see how we can iterate ove
4 min read
How To Create A Csv File Using Python
CSV stands for comma-separated values, it is a type of text file where information is separated by commas (or any other delimiter), they are commonly used in databases and spreadsheets to store information in an organized manner. In this article, we will see how we can create a CSV file using Python
3 min read
Create a GUI to convert CSV file into excel file using Python
Prerequisites: Python GUI â tkinter, Read csv using pandas CSV file is a Comma Separated Value file that uses a comma to separate values. It is basically used for exchanging data between different applications. In this, individual rows are separated by a newline. Fields of data in each row are delim
3 min read
Exporting Multiple Sheets As Csv Using Python
In data processing and analysis, spreadsheets are a common format for storing and manipulating data. However, when working with large datasets or conducting complex analyses, it's often necessary to export data from multiple sheets into a more versatile format. CSV (Comma-Separated Values) files are
3 min read
Calculate Average for Each Row in a CSV file using Python
Working with CSV files is a common task in data analysis and manipulation. Finding the average of each row in a CSV file becomes crucial when dealing with datasets. In this guide, we'll explore how to achieve this using Python, providing a step-by-step approach and practical examples. dataset.csv to
5 min read