Python Program to Check the File Size Using os.path
# Python Program to Check the File Size Using os.path
#importing os module
import os
file_size = os.path.getsize('d:/img1.jpg')
print("File Size is :", file_size, "bytes")
Output:
File Size is : 218 bytes
Python Program to Check the File Size Using stat() Function
# Python Program to Check the File Size Using stat() Function
#importing os module
import os
file_size = os.stat('d:/image1.jpg')
print("Size of file :", file_size.st_size, "bytes")
Output:
Size of file : 200 bytes
Python Program to Check the File Size Using file object
# Python Program to Check the File Size Using file object
# open file
file = open('d:/img.jpg')
# get the cursor positioned at end
file.seek(0, os.SEEK_END)
# get the current position of cursor this will be equivalent to size of file
print("Size of file is :", file.tell(), "bytes")
Output:
Size of file is : 220 bytes
Python Program to Check the File Size Using pathlib
#Python Program to Check the File Size Using pathlib
#importing pathlib module
from pathlib import Path
# open file
Path(r'd:/image.jpg').stat()
# getting file size
file=Path(r'd:/image.jpg').stat().st_size
# display the size of the file
print("Size of file is :", file, "bytes")
Output:
Size of file is : 160 bytes
About The Author
Shailendra Bramhvanshi, Founder at Techaroha Solutions Private Limited and CEO at Newtum Solutions Private Limited. BlockChain Experts with 12 years of IT Experience in of all aspects of running Organization and Project Management. I have created different coins to help the business to bring more trust. Recently deployed a stable coin in India TrueINR and working on few cryptocurrency exchanges. My Team is being rated as a top 10 blockchain service provider in India by Silicon India