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 Set difference()
Python hasattr()
Python Program to Check Prime Number
Python Program to Find Sum of Natural Numbers Using Recursion
Python chr()
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Program to Catch Multiple Exceptions in One Line
Python Program to Check Armstrong Number
Python oct()
Python Dictionary items()
Python Dictionary copy()
Python Dictionary values()
Python Multiple Inheritance
Python list()
Python Program to Append to a File
Python Shallow Copy and Deep Copy
Python Type Conversion and Type Casting
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python String lower()
Python Operator Overloading
Python Program to Make a Simple Calculator
Deep Learning in Python - LazyProgrammer
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python String rsplit()
Python len()
Python tuple()
Python Input, Output and Import
Python List pop()
Python repr()
Python String partition()
Python id()
Python String format_map()