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 Artificial Intelligence Project for Beginners - Joshua Eckroth
Python strptime()
Python Global Keyword
Python String format_map()
Python Custom Exceptions
Python String endswith()
Python Program to Get File Creation and Modification Date
Python Program to Convert Two Lists Into a Dictionary
Python Numbers, Type Conversion and Mathematics
Python Decorators
Python Package
Python Closures
Python String encode()
Python max()
Python Modules
Python Set intersection()
Python Program to Find the Square Root
Python @property decorator
Python File I/O Operation
Python String expandtabs()
Python Program to Shuffle Deck of Cards
Python Program to Measure the Elapsed Time in Python
Python Program to Find All File with .txt Extension Present Inside a Directory
Python ord()
Python enumerate()
Python Program to Add Two Matrices
Python Program to Differentiate Between del, remove, and pop on a List
Python String rstrip()
Python Operators
Python Program to Display the multiplication Table
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python vars()