(Last Updated On: 10/05/2023)
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