The getsize()
function is defined in the path sub-module of the os
module. It is used to retrieve the file size in bytes of a given file or directory.
To use the function, we need to first import it in our program as follows.
from os.path import getsize
Syntax:
getsize(path)
path |
The path to the file whose size we want. |
The path cen be absolute or relative to the working directory.
from os.path import getsize
p = r'C:\users\john\desktop\test.py'
size = getsize(p)
print(size)
Output:
11485
If the path given does not exist, a FilenotFoundError
exception will be raised.