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 moduleScreenshot 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 moduleHere, 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 moduleData 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 moduleData 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
Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read