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 Program to Convert Bytes to a String
Python isinstance()
Python hash()
Python String partition()
Python Program to Display the multiplication Table
Python String rfind()
Python Program to Get the Full Path of the Current Working Directory
Python oct()
Python Program to Trim Whitespace From a String
Python Operator Overloading
Python @property decorator
Python List count()
Python Multiple Inheritance
Python Program to Iterate Over Dictionaries Using for Loop
Python Global, Local and Nonlocal variables
Python String isalpha()
Python List reverse()
Python String lower()
Python globals()
Python RegEx
Python Dictionary items()
Python type()
Python Program to Convert Two Lists Into a Dictionary
Python Program to Find Numbers Divisible by Another Number
Python Program to Slice Lists
Python Program to Create Pyramid Patterns
Python String isdecimal()
Python delattr()
Python ascii()
Python strptime()
Python Program to Find HCF or GCD
Python sleep()