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 setattr()
APIs in Node.js vs Python - A Comparison
Python locals()
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Get File Creation and Modification Date
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python Program to Convert Decimal to Binary Using Recursion
Python String join()
Python Modules
Python Program to Display Powers of 2 Using Anonymous Function
Python Program to Randomly Select an Element From the List
Python Program to Count the Number of Occurrence of a Character in String
Python Program to Count the Number of Each Vowel
Python Decorators
Python Errors and Built-in Exceptions
Python Set union()
Python print()
Python Set update()
Python String zfill()
Python Global, Local and Nonlocal variables
Deep Learning with Python - Francois Cholletf
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python String partition()
Python Program to Check if a Number is Odd or Even
Python Tuple count()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python datetime
Python format()
Python Deep Learning Cookbook - Indra den Bakker
Python Machine Learning Eqution Reference - Sebastian Raschka
Python while Loop
Python Set remove()