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 Remove Punctuations From a String
Python int()
Python any()
Python Program to Print Output Without a Newline
Python timestamp to datetime and vice-versa
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python Program to Count the Number of Digits Present In a Number
Python delattr()
Deep Learning in Python - LazyProgrammer
Python Machine Learning Eqution Reference - Sebastian Raschka
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python Modules
Python abs()
Python Program to Add Two Numbers
Introduction to Scientific Programming with Python - Joakim Sundnes
Python Program to Merge Mails
Python input()
Python Program to Count the Number of Each Vowel
Python Program to Display Fibonacci Sequence Using Recursion
Python Program to Return Multiple Values From a Function
Python Program to Check Prime Number
Python String rindex()
Python str()
Python Program to Check If a String Is a Number (Float)
Deep Learning with Python - Francois Chollet
Python property()
Python Program to Find Armstrong Number in an Interval
Python Program to Find the Size (Resolution) of a Image
Python Program to Delete an Element From a Dictionary
Python String isdigit()
Python Program to Get a Substring of a String
Python Program to Remove Duplicate Element From a List