How To Get The Uncompressed And Compressed File Size Of A File In Python
Last Updated :
27 Feb, 2024
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?
Uncompressed File: An uncompressed file is a file that has not undergone any form of compression. The size of an uncompressed file represents the total amount of data it contains in its original form. Common file formats like text files (.txt), image files (.png, .jpg), and document files (.pdf, .docx) are typically uncompressed.
Compressed File: A compressed file, on the other hand, has undergone a compression process to reduce its size. Compression is the technique of encoding information in a way that takes up less space. Common compressed file formats include ZIP (.zip), Gzip (.gz), and Bzip2 (.bz2). These formats use various algorithms to compress the original data, resulting in smaller file sizes.
How to Get the Uncompressed and Compressed File Size of a File in Python
Below are the methods of How To Get The Uncompressed And Compressed File Size Of A File In Python:
Using the os and gzip Modules
In this example, below code defines a function `get_file_sizes` that calculates the uncompressed and compressed sizes of a specified file using `os.path.getsize` and the `zipfile` module. It iterates through entries in a ZIP file, excluding directories, and returns a tuple with the sizes. The example usage demonstrates obtaining and printing these sizes for a specific file path.
Python3
import os
import zipfile
def get_file_sizes(file_path):
uncompressed_size = os.path.getsize(file_path)
with zipfile.ZipFile(file_path, 'r' ) as zip_file:
compressed_size = 0
for entry in zip_file.infolist():
if not entry.is_dir():
compressed_size + = entry.file_size
return uncompressed_size, compressed_size
file_path = r 'add\file\path'
uncompressed_size, compressed_size = get_file_sizes(file_path)
print (f 'Uncompressed Size: {uncompressed_size} bytes' )
print (f 'Compressed Size: {compressed_size} bytes' )
|
Output:
Uncompressed Size: 7909996 bytes
Compressed Size: 21017332 bytes
Using the zstandard Library
In this example, below Python code defines a function get_file_sizes to calculate the uncompressed and compressed sizes of a file specified by the file_path. It utilizes the os.path.getsize method to determine the uncompressed size and the zstandard library to compress the file and obtain the compressed size. The function returns a tuple containing the uncompressed and compressed sizes.
Python3
import os
import zstandard as zstd
def get_file_sizes(file_path):
uncompressed_size = os.path.getsize(file_path)
with open (file_path, 'rb' ) as file :
compressed_data = zstd.compress( file .read())
compressed_size = len (compressed_data)
return uncompressed_size, compressed_size
file_path = r 'add\file\path'
uncompressed_size, compressed_size = get_file_sizes(file_path)
print (f 'Uncompressed Size: {uncompressed_size} bytes' )
print (f 'Compressed Size: {compressed_size} bytes' )
|
Output:
Uncompressed Size: 7909996 bytes
Compressed Size: 7450566 bytes
Share your thoughts in the comments
Please Login to comment...