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 Set add()
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python Dictionary get()
Python String find()
Python Program to Check Prime Number
Python Program to Slice Lists
Python List copy()
Python Program to Create Pyramid Patterns
Python Program to Parse a String to a Float or Int
Python while Loop
Python Dictionary
Python zip()
How to get current date and time in Python?
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python Shallow Copy and Deep Copy
Python Program to Generate a Random Number
Python format()
Python Program to Shuffle Deck of Cards
Python callable()
Python Program to Concatenate Two Lists
Python Program to Catch Multiple Exceptions in One Line
Python classmethod()
Python len()
Python Program to Get the Class Name of an Instance
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python Program to Differentiate Between type() and isinstance()
Python Program to Access Index of a List Using for Loop
Python bin()
Python String title()
Python Program to Find All File with .txt Extension Present Inside a Directory
Python setattr()
Python Custom Exceptions