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 hex()
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python property()
Python Program to Slice Lists
Python Program to Convert String to Datetime
Python Matrices and NumPy Arrays
Python Program to Capitalize the First Character of a String
Python Program to Access Index of a List Using for Loop
Python Statement, Indentation and Comments
Python Program to Count the Occurrence of an Item in a List
Python Program to Find the Square Root
Python slice()
Python reversed()
Python Exception Handling Using try, except and finally statement
Python oct()
Python Program to Sort Words in Alphabetic Order
Python zip()
Python Functions
Python Program to Check Prime Number
Python List copy()
Python Program to Represent enum
Python Program to Check if a Number is Odd or Even
Python Program to Calculate the Area of a Triangle
Python String rpartition()
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Compute the Power of a Number
Python sleep()
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python next()
Python String isspace()
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Program to Check if a Number is Positive, Negative or 0