AppDividend
Latest Code Tutorials

Three Ways to Get File Size in Python

0

Python stat() is an inbuilt os module that has two methods that return the file size of a file. OS module in Python provides functions for interacting with the operating system. The OS module comes under Python’s standard utility modules. The os module provides a portable approach to using operating system dependent functionality.

Python get the file size

To find the size of a file in Python, we can use one of the following three methods.

  1. Python os.path.getsize()
  2. Python os.stat()
  3. Python path.stat().st_mode

Python os.path.getsize()

The os.path.getsize() function returns the size, in bytes, of the Path. Raise OSError if the file does not exist or is inaccessible.

See the following code.

import os

file_name = "npfile.txt"

file_size = os.path.getsize(file_name)

print(f'File Size in Bytes is {file_size}')
print(f'File Size in MegaBytes is {file_size / (1024 * 1024)}')

Output

File Size in Bytes is 42
File Size in MegaBytes is 4.00543212890625e-05

First, we have defined a file and then get the file size using os.path.getsize() function, which returns file size in bytes, and then in the last line, we have converted the bytes size to MB size.

Python os.stat()

The os.stat() method in Python performs stat() system call on the specified Path. The stat() method is used to get the status of the specified Path. We can then get its st_size attribute to get the file size in bytes. The stat() method takes an argument as the filename and will return the tuple that contains the information about the file.

import os

file_name = "npfile.txt"

file_stats = os.stat(file_name)

print(file_stats)
print(f'File Size in Bytes is {file_stats.st_size}')
print(f'File Size in MegaBytes is {file_stats.st_size / (1024 * 1024)}')

Output

os.stat_result(st_mode=33188, st_ino=75203319, st_dev=16777220, 
st_nlink=1, st_uid=501, st_gid=20, st_size=42, 
st_atime=1589517754, st_mtime=1589517753, 
st_ctime=1589517753)
File Size in Bytes is 42
File Size in MegaBytes is 4.00543212890625e-05

From the output, you can see that, and we got the tuple full of file information, and then we have accessed particular property called st_size to get the file size and then convert the size into MB or Megabytes. 

If you closely look at the stat() method, we can pass two more parameters: dir_fd and follow_symlinks. However, they are not implemented for macOS.

Python path.stat().st_mode

Python path.stat() function returns the os.stat_result object containing information about this path, like os.stat(). The result is looked up at each call to this method.

See the following code.

from pathlib import Path

file_name = "npfile.txt"

file_size = Path(file_name).stat().st_size

print(f'File Size in Bytes is {file_size}')
print(f'File Size in MegaBytes is {file_size / (1024 * 1024)}')

Output

File Size in Bytes is 42
File Size in MegaBytes is 4.00543212890625e-05

First, we have imported the pathlib module and use the Path.state().st_size to get the file size.

So, we have seen all the three methods to get file size in Python.

That is it for the file size in Python tutorial.

See also

How to open a file in Python

How to read a file in Python

How to write a file in Python

How to rename a file in Python

How to move a file in Python

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.