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 Generate a Random Number
Python String isnumeric()
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python property()
Python Program to Find Hash of File
Python list()
Python Anonymous / Lambda Function
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python Program to Count the Number of Each Vowel
Python Program to Convert Two Lists Into a Dictionary
Python strptime()
Python print()
Python List reverse()
Python Program to Get a Substring of a String
Python Program to Check Prime Number
Python List remove()
Python Set union()
Python abs()
Python Program to Reverse a Number
Python setattr()
Python Program to Get the File Name From the File Path
Python Program to Find Factorial of Number Using Recursion
Python format()
Python Operators
Python Closures
Python bin()
Python id()
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python Program to Print Output Without a Newline
Python Program to Copy a File
How to get current date and time in Python?
Python globals()