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 bin()
Python sum()
Python Deep Learning Cookbook - Indra den Bakker
Python Shallow Copy and Deep Copy
Python Program to Create Pyramid Patterns
Python List Comprehension
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python Program to Create a Long Multiline String
Python Program to Trim Whitespace From a String
Python range()
Machine Learning with Python for everyone - Mark E.Fenner
Python float()
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python Program to Get Line Count of a File
Python Program to Find the Largest Among Three Numbers
Python isinstance()
Python Namespace and Scope
Python Program to Delete an Element From a Dictionary
Python Program to Differentiate Between type() and isinstance()
Python Set intersection()
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python abs()
Python String isnumeric()
Python String splitlines()
Python hex()
Python String format_map()
Python Program to Solve Quadratic Equation
Python Program to Compute all the Permutation of the String
Python enumerate()
Python Global Keyword
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python String capitalize()