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 String zfill()
Python Program to Transpose a Matrix
Python Program to Get File Creation and Modification Date
Python *args and **kwargs
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python Program to Count the Number of Each Vowel
Python pass statement
Python Program to Trim Whitespace From a String
Python String expandtabs()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python Multiple Inheritance
Python hex()
Python issubclass()
Python break and continue
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python List count()
Python Program to Safely Create a Nested Directory
Python Numbers, Type Conversion and Mathematics
Python Program to Display the multiplication Table
Python Set add()
Python timestamp to datetime and vice-versa
Python Program to Iterate Over Dictionaries Using for Loop
Python List reverse()
Python Program to Check Whether a String is Palindrome or Not
Python String rjust()
Python Program to Compute all the Permutation of the String
Python String capitalize()
Python Program to Remove Punctuations From a String
Python classmethod()
Python Program to Find the Factors of a Number
Python slice()
Python String rindex()