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:
Python Program to Get File Creation and Modification Date
Python min()
Python callable()
Python open()
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python range()
Python Program to Represent enum
Deep Learning with Python - Francois Chollet
Python Program to Reverse a Number
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Machine Learning with Python for everyone - Mark E.Fenner
Python Program to Append to a File
Python Program to Differentiate Between del, remove, and pop on a List
Python String partition()
Python next()
Python File I/O Operation
Python Program to Find LCM
Python chr()
Python Program to Check Armstrong Number
Python setattr()
Python List count()
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python map()
Python frozenset()
Python String title()
Python List
Python Program to Convert Kilometers to Miles
Python Program to Solve Quadratic Equation
Python hasattr()
Python any()
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python Program to Display Calendar