Getting file size in Python? [duplicate] Getting file size in Python? [duplicate] python python

Getting file size in Python? [duplicate]


Use os.path.getsize(path) which will

Return the size, in bytes, of path. Raise OSError if the file does not exist or is inaccessible.

import osos.path.getsize('C:\\Python27\\Lib\\genericpath.py')

Or use os.stat(path).st_size

import osos.stat('C:\\Python27\\Lib\\genericpath.py').st_size 

Or use Path(path).stat().st_size (Python 3.4+)

from pathlib import PathPath('C:\\Python27\\Lib\\genericpath.py').stat().st_size


os.path.getsize(path)

Return the size, in bytes, of path. Raise os.error if the file does not exist or is inaccessible.


You may use os.stat() function, which is a wrapper of system call stat():

import osdef getSize(filename):    st = os.stat(filename)    return st.st_size


matomo