Python – Get File Size
To get file size in Python, you can use the os.path module to get the size of a file. Call os.path.getsize() function and pass the file path as argument to the function. The function returns the size of specified file in bytes.
The syntax to call os.path.getsize() function to get the size of a file at file_path location is
import os
os.path.getsize(file_path)
Example
In the following program, we will get the size of the file data.txt programmatically using os.path.getsize() function.
File
Python Program
import os
file_path = 'myfolder/data.txt'
file_size = os.path.getsize(file_path)
print(file_size)
CopyOutput
12
Summary
In this tutorial of Python Examples, we learned how to get the size of a file in bytes using os.path.getsize() function, with the help of examples.