How to Check the Size of a Zip File in Python?

Estimated read time 2 min read

You can check the size of a zip file in Python by using the os.path.getsize() function. Here’s an example:

import os

zip_file_path = "path/to/your/zip/"

# Get the size of the zip file in bytes
zip_file_size = os.path.getsize(zip_file_path)

# Convert the size to a human-readable format
if zip_file_size < 1024:
    zip_file_size_str = f"{zip_file_size} bytes"
elif zip_file_size < 1048576:
    zip_file_size_str = f"{zip_file_size/1024:.2f} KB"
    zip_file_size_str = f"{zip_file_size/1048576:.2f} MB"

print(f"The size of the zip file is {zip_file_size_str}")

In this example, the os.path.getsize() function is used to get the size of the zip file located at zip_file_path. The size is returned in bytes, which is then converted to a human-readable format using a simple if-else block. The resulting size is then printed to the console using the print() function.

You can adjust the file path to the location of your zip file, and modify the output format of the size string as per your requirements.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply