How to get file size in Python? - GeeksforGeeks

21 Jan, 2021
We can follow different approaches to get the file size in Python. It’s important to get the file size in Python to monitor file size or in case of ordering files in the directory according to file size. 
Method 1: Using getsize function of os.path module
This function takes a file path as an argument and it returns the file size (bytes). 
Example:
Python3
# approach 1# using getsize function os.path moduleimport os file_size = os.path.getsize('d:/file.jpg')print("File Size is :", file_size, "bytes")
Output:
File Size is : 218 bytes
Method 2: Using stat function of the OS module
This function takes a file path as an argument (string or file object) and returns statistical details about file path given as input. 
Example:
Python3
# approach 2# using stat function of os moduleimport os file_size = os.stat('d:/file.jpg')print("Size of file :", file_size.st_size, "bytes")
Output:
Size of file : 218 bytes
Method 3: Using File Object
To get the file size, follow these steps –
Use the open function to open the file and store the returned object in a variable.  When the file is opened, the cursor points to the beginning of the file.File object has seek method used to set the cursor to the desired location. It accepts 2 arguments – start location and end location. To set the cursor at the end location of the file use method os.SEEK_END.File object has tell method that can be used to get the current cursor location which will be equivalent to the number of bytes cursor has moved. So this method actually returns the size of the file in bytes.
Use the open function to open the file and store the returned object in a variable.  When the file is opened, the cursor points to the beginning of the file.
File object has seek method used to set the cursor to the desired location. It accepts 2 arguments – start location and end location. To set the cursor at the end location of the file use method os.SEEK_END.
File object has tell method that can be used to get the current cursor location which will be equivalent to the number of bytes cursor has moved. So this method actually returns the size of the file in bytes.
Example:
Python3
# approach 3# using file object # open filefile = open('d:/file.jpg') # get the cursor positioned at endfile.seek(0, os.SEEK_END) # get the current position of cursor# this will be equivalent to size of fileprint("Size of file is :", file.tell(), "bytes")
Output:
Size of file is : 218 bytes
Method 4: Using Pathlib Module
The stat() method of the Path object returns st_mode, st_dev, etc. properties of a file. And, st_size attribute of the stat method gives the file size in bytes.
Example:
Python3
# approach 4# using pathlib modulefrom pathlib import Path # open filePath(r'd:/file.jpg').stat() # getting file sizefile=Path(r'd:/file.jpg').stat().st_size # display the size of the fileprint("Size of file is :", file, "bytes") # this code was contributed by debrc
Output:
Size of file is : 218 bytes
debrc
Python file-handling-programs
python-file-handling
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python Classes and Objects
Create a directory in Python
Python | Pandas dataframe.groupby()
Python | os.path.join() method
Python | Get unique values from a list