How to Get File Size in Python?

There are different ways to get file size in Python. We will be using the os module and the pathlib module to check the file size. OS module in Python comes as built-in, and it provides various utility methods to interact with operating system features.

Python get file size

The popular ways to fetch file size in Python are as follows. You could use this technique to get multiple file sizes in a specified directory. These methods can be used on file type extensions to fetch the file size.

  • os.path.getsize()
  • os.stat()
  • seek() and tell()
  • path.stat().st_mode

Method 1: Get file size using os.path.getsize()

The os.path.getsize() function takes a file path as an argument and returns the file size in bytes. If the function cannot find the file or is inaccessible, or if file is deleted, Python will raise an OSError

# Import os module
import os

# set the file path
file = "python.pdf"

# Get the file size using os.path.getsize() function
file_size = os.path.getsize(file)

print('File Size in Bytes is ', file_size)
print('File Size in KiloBytes is ', (file_size / 1024))
print('File Size in MegaBytes is ', (file_size / (1024 * 1024)))

Output

File Size in Bytes is  12271318
File Size in KiloBytes is  11983.708984375
File Size in MegaBytes is  11.702840805053711

Method 2: Get file size using os.stat()

The os.stat() function takes a file path as an argument and returns the statistical details of the file as a tuple. The stat() method will get the status of the specified file path, and the st_size attribute will fetch the file size in bytes.

# Import os module
import os

# set the file path
file = "python.pdf"

#If you want to print the file info 
file_info= os.stat(file)
print(file_info)

# Get the file size using os.stat() function
file_size = os.stat(file).st_size

print('File Size in Bytes is ', file_size)
print('File Size in KiloBytes is ', (file_size / 1024))
print('File Size in MegaBytes is ', (file_size / (1024 * 1024)))

Output

os.stat_result(st_mode=33206, st_ino=12103423998770118, st_dev=3351013, st_nlink=1, st_uid=0, st_gid=0, st_size=12271318, st_atime=1632686420, st_mtime=1632608049, st_ctime=1632686420)
File Size in Bytes is  12271318
File Size in KiloBytes is  11983.708984375
File Size in MegaBytes is  11.702840805053711

Method 3: Get file size using seek() and tell()

The other methods perfectly work in the case of an actual file, and if you have something like file-like objects, you could use seek() and tell() to fetch the file size.

There are three steps involved to get the file size.

Step 1: Open the file using the open() function and store the return object into a variable. When the file is opened, the cursor always points to the beginning of the file.

Step 2: File objects provide a seek() method to set the cursor into the desired location. It accepts two arguments start position and the end position. To set the cursor at the end location of the file, use method os.SEEK_END.

Step 3: The file object has a tell() method that can get the current cursor position and provides the number of bytes it has moved from the initial position. Basically, it gives the actual file size in bytes format.


# Import os module
import os

# set the file path
file_name = "python.pdf"
 
# open file using open() function
file = open(file_name)
 
# set the cursor position to end of file
file.seek(0, os.SEEK_END)

# get the current position of cursor
# this will be equivalent to size of file
file_size= file.tell()
 

print('File Size in Bytes is ', file_size)
print('File Size in KiloBytes is ', (file_size / 1024))
print('File Size in MegaBytes is ', (file_size / (1024 * 1024)))

Output

File Size in Bytes is  12271318
File Size in KiloBytes is  11983.708984375
File Size in MegaBytes is  11.702840805053711

Method 4: Get file size using path.stat().st_mode

The stat() method of the Path object returns the properties of a file like st_modest_dev, etc. and, the st_size attribute of the stat method returns the file size in bytes.

# Import pathlib module
import pathlib

# set the file path
file = "python.pdf"

# Get the file size using pathlib.Path() function
file_size = pathlib.Path(file).stat().st_size

print('File Size in Bytes is ', file_size)
print('File Size in KiloBytes is ', (file_size / 1024))
print('File Size in MegaBytes is ', (file_size / (1024 * 1024)))

Output

File Size in Bytes is  12271318
File Size in KiloBytes is  11983.708984375
File Size in MegaBytes is  11.702840805053711

Note: The pathlib module is available only from Python 3.4 and above versions.

All the above methods provide the file size in bytes format. In most cases, if the file size is significant, you would need it in a human-readable format as kilobytes or megabytes.

Python get file size in kb (KiloBytes)

To convert from bytes to Kilobytes, divide the filesize bytes by 1024, as shown in the above examples.

Python get file size in kb (MegaBytes)

To convert from bytes to Megabytes, divide the filesize bytes by (1024 x 1024) as shown in the above examples.

Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Typeerror Unhashable Type 'Dict'

TypeError: unhashable type: ‘dict’

Table of Contents Hide TypeError: unhashable type: ‘dict’Solution to TypeError: unhashable type: ‘dict’.By Converting into a tupleBy Adding Dictionary as a value in another dictionary In Python, all the dictionary…
View Post
How To Rename A File In Python

How to Rename a file in Python?

Table of Contents Hide Python Rename FileExample to rename a file in PythonPython Rename Multiple FilesExample to Rename Multiple Files in Python The os module in Python comes in handy…
View Post
How To Fix Keyerror In Pandas

How to Fix: KeyError in Pandas?

The KeyError in Pandas occurs when you try to access the columns in pandas DataFrame, which does not exist, or you misspell them.  Typically, we import data from the excel…
View Post
Python String Zfill()

Python String zfill()

Table of Contents Hide zfill() Syntaxzfill() Parameterszfill() Return ValueExample 1: How zfill() works in Python?Example 2: zfill() in case of Sign Prefix The Python String zfill() method is a built-in…
View Post