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 List extend()
Python Program to Convert Celsius To Fahrenheit
Python Variables, Constants and Literals
Python Inheritance
Python Program to Capitalize the First Character of a String
Python Set issubset()
Python id()
Python Program to Get the File Name From the File Path
Python Program to Randomly Select an Element From the List
Python Set intersection()
Python Program to Find the Size (Resolution) of a Image
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python Program to Make a Flattened List from Nested List
Python List pop()
Python Set copy()
Python Directory and Files Management
Python Program to Check If a String Is a Number (Float)
Python Dictionary copy()
Python range()
Python datetime
Python Errors and Built-in Exceptions
Python bytearray()
Python String center()
Python Custom Exceptions
How to Get Started With Python?
Python Program to Copy a File
Python Objects and Classes
Python Program to Differentiate Between type() and isinstance()
Python Set remove()
Python Program to Get the Full Path of the Current Working Directory
Python Data Types
Python Program to Trim Whitespace From a String