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 Swap Two Variables
Python Dictionary popitem()
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python Program to Convert Kilometers to Miles
Python Program to Measure the Elapsed Time in Python
Python callable()
Python Program to Merge Two Dictionaries
Python String rfind()
Deep Learning with Python - Francois Chollet
Python String encode()
Python Set remove()
Python Program to Check Whether a String is Palindrome or Not
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Intelligent Projects Using Python - Santanu Pattanayak
Python dir()
Python Program to Check Prime Number
Python Program to Check if a Number is Positive, Negative or 0
Python String isdecimal()
Python Program to Create a Long Multiline String
Python List Comprehension
Python enumerate()
Python Program to Generate a Random Number
Python Program to Compute all the Permutation of the String
Python Set copy()
Python Operators
Python Program to Find the Largest Among Three Numbers
Python getattr()
Python range()
Python Deep Learning Cookbook - Indra den Bakker
Python zip()
How to Get Started With Python?