Home » How to Check the File Size in python

How to Check the File Size in python

In this example, you will learn to check the file size in python.

We will show 2 different ways to do this

Example 1

Using stat() from the os module, you can get the details of a file.

We then use the st_size attribute of stat() method to get the file size. The file size is returned in bytes.

import os

file_stat = os.stat('readme.txt')
print(file_stat.st_size)

When you run this you will see something like this

>>> %Run filesize1.py
2797

Example 2

from pathlib import Path

file = Path('readme.txt')
print(file.stat().st_size)

When you run this you will see something like this

>>> %Run filesize2.py
2797

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More