Table of Contents
In this example, you will learn to check the file size.
To understand this example, you should have the knowledge of the following Python programming topics:
1. 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.
2. 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.
Related posts:
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python del Statement
Python String isprintable()
Python String zfill()
Python String rfind()
Python Dictionary popitem()
Python Dictionary get()
Python String lower()
Python Iterators
Python Program to Sort Words in Alphabetic Order
Python print()
Python len()
Python @property decorator
Python Global, Local and Nonlocal variables
Python Anonymous / Lambda Function
Python Program to Check Leap Year
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python String format_map()
Python while Loop
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python Dictionary clear()
Python oct()
Python List
Intelligent Projects Using Python - Santanu Pattanayak
Python String startswith()
Python String capitalize()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python Set clear()
Python Set intersection()
Python Program to Swap Two Variables
Python String swapcase()
Python Shallow Copy and Deep Copy