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 super()
Python Set intersection_update()
Python Keywords and Identifiers
Python String rstrip()
Python Program to Transpose a Matrix
Python Program to Get Line Count of a File
Python Sets
Python Set issubset()
Python Set symmetric_difference()
Intelligent Projects Using Python - Santanu Pattanayak
Python Objects and Classes
Python Program to Convert String to Datetime
Python Closures
Debug a JavaMail Program
Python id()
Python Program to Find ASCII Value of Character
Python Program to Find HCF or GCD
Python String translate()
Python String ljust()
Python Dictionary get()
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python Program to Calculate the Area of a Triangle
How to Get Started With Python?
Python Custom Exceptions
Java Program to Implement the Program Used in grep/egrep/fgrep
Python staticmethod()
Python File I/O Operation
Python Program to Copy a File
Python List copy()
Python Set add()
Python Decorators