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 print()
Python Program to Copy a File
Python Program to Print the Fibonacci sequence
Python Strings
Python time Module
Python String encode()
Python del Statement
Python Program to Check if a Key is Already Present in a Dictionary
Python all()
Python List sort()
Python Dictionary copy()
Python Program to Measure the Elapsed Time in Python
Python Program to Convert Kilometers to Miles
Python String isnumeric()
Python Program to Check Armstrong Number
Python List Comprehension
Python String rjust()
Python Dictionary fromkeys()
Python Program to Display the multiplication Table
Python str()
How to get current date and time in Python?
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Find All File with .txt Extension Present Inside a Directory
Python min()
Python Objects and Classes
Python String endswith()
Python RegEx
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python Program to Get File Creation and Modification Date
Python Program to Iterate Over Dictionaries Using for Loop
Python Program to Trim Whitespace From a String
Python Program to Find Armstrong Number in an Interval