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 Get the File Name From the File Path
Python Program to Sort Words in Alphabetic Order
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python List pop()
Python String isnumeric()
Python Program to Safely Create a Nested Directory
Python Program to Append to a File
Python Operators
Python timestamp to datetime and vice-versa
Python Data Types
Python Variables, Constants and Literals
Python Program to Convert String to Datetime
Python String islower()
Python Objects and Classes
Python Multiple Inheritance
Python String partition()
Python Program to Calculate the Area of a Triangle
Python zip()
Python all()
Python String isdigit()
Python String isidentifier()
Python Program to Solve Quadratic Equation
Python memoryview()
Python chr()
Python Set difference_update()
Python isinstance()
Python Program to Check Leap Year
Python next()
Python Program to Count the Number of Each Vowel
Python String maketrans()
Python repr()
Python datetime