Python Program to Check the File Size

To understand this example, you should have the knowledge of the following Python programming topics:


Example 1: Using os module

import os

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

Output

34

Using stat() from the os module, you can get the details of a file. Use the st_size attribute of stat() method to get the file size.

The unit of the file size is byte.


Example 2: Using pathlib module

from pathlib import Path

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

Output

34

Using the pathlib module, you can do the same thing as shown above. The unit of the file size is byte.


Also Read:

Did you find this article helpful?

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community