23
loading...
This website collects cookies to deliver better user experience
pathlib
module to check the file size. OS module in Python comes as built-in, and it provides various utility methods to interact with operating system features. os.path.getsize()
function takes a file path as an argument and returns the file size in bytes. If the function cannot find the file or is inaccessible, Python will raise an OSError.# Import os module
import os
# set the file path
file = "python.pdf"
# Get the file size using os.path.getsize() function
file_size = os.path.getsize(file)
print('File Size in Bytes is ', file_size)
print('File Size in KiloBytes is ', (file_size / 1024))
print('File Size in MegaBytes is ', (file_size / (1024 * 1024)))
File Size in Bytes is 12271318
File Size in KiloBytes is 11983.708984375
File Size in MegaBytes is 11.702840805053711
os.stat()
function takes a file path as an argument and returns the statistical details of the file as a tuple. The stat()
method will get the status of the specified file path, and the st_size
attribute will fetch the file size in bytes.# Import os module
import os
# set the file path
file = "python.pdf"
#If you want to print the file info
file_info= os.stat(file)
print(file_info)
# Get the file size using os.stat() function
file_size = os.stat(file).st_size
print('File Size in Bytes is ', file_size)
print('File Size in KiloBytes is ', (file_size / 1024))
print('File Size in MegaBytes is ', (file_size / (1024 * 1024)))
os.stat_result(st_mode=33206, st_ino=12103423998770118, st_dev=3351013, st_nlink=1, st_uid=0, st_gid=0, st_size=12271318, st_atime=1632686420, st_mtime=1632608049, st_ctime=1632686420)
File Size in Bytes is 12271318
File Size in KiloBytes is 11983.708984375
File Size in MegaBytes is 11.702840805053711
seek()
and tell()
to fetch the file size.open()
function and store the return object into a variable. When the file is opened, the cursor always points to the beginning of the file.seek()
method to set the cursor into the desired location. It accepts two arguments start position and the end position. To set the cursor at the end location of the file, use method _ os.SEEK_END
._tell()
method that can get the current cursor position and provides the number of bytes it has moved from the initial position. Basically, it gives the actual file size in bytes format.# Import os module
import os
# set the file path
file_name = "python.pdf"
# open file using open() function
file = open(file_name)
# set the cursor position to end of file
file.seek(0, os.SEEK_END)
# get the current position of cursor
# this will be equivalent to size of file
file_size= file.tell()
print('File Size in Bytes is ', file_size)
print('File Size in KiloBytes is ', (file_size / 1024))
print('File Size in MegaBytes is ', (file_size / (1024 * 1024)))
File Size in Bytes is 12271318
File Size in KiloBytes is 11983.708984375
File Size in MegaBytes is 11.702840805053711
stat()
method of the Path object returns the properties of a file like st_mode
, st_dev
, etc. and, the st_size
attribute of the stat method returns the file size in bytes.# Import pathlib module
import pathlib
# set the file path
file = "python.pdf"
# Get the file size using pathlib.Path() function
file_size = pathlib.Path(file).stat().st_size
print('File Size in Bytes is ', file_size)
print('File Size in KiloBytes is ', (file_size / 1024))
print('File Size in MegaBytes is ', (file_size / (1024 * 1024)))
File Size in Bytes is 12271318
File Size in KiloBytes is 11983.708984375
File Size in MegaBytes is 11.702840805053711
pathlib
module is available only from *Python 3.4 and above * versions.