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
copy
Syntax:
getsize(path)
copy
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)
copy
Output:
11485
If the path given does not exist, a FilenotFoundError
exception will be raised.