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 Check if a Number is Positive, Negative or 0
Python List index()
Python String rpartition()
Python Dictionary popitem()
Python strptime()
Python Global Keyword
Python Program to Reverse a Number
Python Objects and Classes
Python Deep Learning Cookbook - Indra den Bakker
Python Program to Measure the Elapsed Time in Python
Python Program to Create Pyramid Patterns
Python Program to Differentiate Between type() and isinstance()
Python ord()
Python Program to Check if a Number is Odd or Even
Python String expandtabs()
Python Recursion
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python List reverse()
Debug a JavaMail Program
Python Anonymous / Lambda Function
Python String rindex()
Python Modules
Python memoryview()
Python Dictionary values()
Python Set intersection()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python sum()
Python Program to Convert Celsius To Fahrenheit
Python String istitle()
Python List append()
Python Type Conversion and Type Casting
Python Inheritance