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 Find Sum of Natural Numbers Using Recursion
Python Program to Merge Mails
Python String isdigit()
Python Program to Check Armstrong Number
Python Functions
Python callable()
Python Program to Append to a File
Python Tuple index()
Python Modules
Python dir()
Python Program to Concatenate Two Lists
Python List remove()
Python Program to Generate a Random Number
Python Program to Print the Fibonacci sequence
Python Global Keyword
Python compile()
Python input()
Python Package
Python globals()
Python String splitlines()
Python Program to Get Line Count of a File
How to Get Started With Python?
Python String rfind()
Python Matrices and NumPy Arrays
Python sorted()
Python Set difference()
Python slice()
Deep Learning with Python - Francois Chollet
Python getattr()
Python Program to Make a Flattened List from Nested List
Python Deep Learning Cookbook - Indra den Bakker
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...