How to Determine the Size of a File in Python

82 views

How to Determine the Size of a File in Python

How to Determine the Size of a File in Python

I need to find out the size of a file in Python. How can I achieve this?

solveurit24@gmail.com Changed status to publish February 16, 2025
0

You can use the os.path.getsize() function from the os module to get the size of a file in bytes.

Code Example:

import os

file_path = "example.txt"
file_size = os.path.getsize(file_path)
print(f"The size of {file_path} is {file_size} bytes.")


This code retrieves the size of the file specified by file_path and prints it in bytes.

solveurit24@gmail.com Changed status to publish February 16, 2025
0