How to check file size in Python?
Given a file, the task here is to generate a Python Script to print its size. This article explains 2 methods to do so.
- Import module
- Get file size
File in use
Size: 226 bytes
Method1: Using pathlib
Path().stat().st_size() function of pathlib module gets the size of any type of the file and the output of this function will be the size of the file in bytes.
Method 2: With Os module
os.path.getsize() function only works with os Library, with the help of importing this library we can use this to get the size of any type of file and the output of this function will be the size of the file in bytes.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course