740
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