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 Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python List clear()
Python String startswith()
Python Program to Extract Extension From the File Name
Python strptime()
Python globals()
Python Program to Find the Square Root
Python List copy()
Python len()
Python enumerate()
Python Anonymous / Lambda Function
Python Exception Handling Using try, except and finally statement
Python String partition()
Python Global, Local and Nonlocal variables
Python Custom Exceptions
Python Multiple Inheritance
Python dict()
Python String strip()
Python Program to Print all Prime Numbers in an Interval
Python String rsplit()
Python List count()
Python Program to Remove Punctuations From a String
Python classmethod()
Python Program to Access Index of a List Using for Loop
Python divmod()
Node.js vs Python for Backend Development
Python Program to Slice Lists
Python Program to Check if a Number is Positive, Negative or 0
Python Set issubset()
Python List pop()
Python Program to Copy a File
Python repr()