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 abs()
Python Dictionary clear()
Python memoryview()
Python min()
Python Program to Find Sum of Natural Numbers Using Recursion
APIs in Node.js vs Python - A Comparison
Python property()
Python dir()
Python complex()
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python Program to Print Hello world!
Python Set issubset()
Python String isdecimal()
Python Set isdisjoint()
Python Program to Find LCM
Python if...else Statement
Python Program to Find the Size (Resolution) of a Image
Deep Learning in Python - LazyProgrammer
Python range()
Python Set add()
Python bytes()
Python Program Read a File Line by Line Into a List
Python String translate()
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python Dictionary pop()
Python Program to Reverse a Number
Python Directory and Files Management
Python Program to Find Hash of File
Python Program to Concatenate Two Lists
Python Errors and Built-in Exceptions
Python set()
Python Custom Exceptions