Get the File/Directory Size in Python: os.path.getsize
In Python, os.path.getsize()
allows you to get the file size in bytes. Additionally, os.scandir()
can be used to calculate the total size of all files within a directory (folder).
For retrieving timestamps, such as creation or modification dates, see the following article.
Get file size with os.path.getsize()
You can get the file size in bytes with os.path.getsize()
.
Specify the file path using a string or a path-like object like pathlib.Path
.
import os
print(os.path.getsize('data/src/lena_square.png'))
# 473831
Get directory size with os.scandir()
(Python 3.5 or later)
To calculate the total size of files in a directory (folder), use os.scandir()
.
Note that os.scandir()
was added in Python 3.5. Use os.listdir()
for earlier versions, as discussed in the next section.
Define a function as follows.
import os
def get_dir_size(path='.'):
total = 0
with os.scandir(path) as it:
for entry in it:
if entry.is_file():
total += entry.stat().st_size
elif entry.is_dir():
total += get_dir_size(entry.path)
return total
print(get_dir_size('data/src'))
# 56130856
This function uses os.scandir()
to iterate over os.DirEntry
objects, representing files and directories in the specified directory.
For each entry, the is_file()
and is_dir()
methods check if it is a file or a directory. For files, the size is acquired from the st_size
attribute of the stat_result
object returned by the stat()
method. For directories, this function is called recursively to sum the sizes of all contained files and subdirectories.
By default, is_file()
and is_dir()
return True
for symbolic links to files and directories, respectively. To ignore symbolic links, set the follow_symlinks
argument to False
in both methods.
To exclude subdirectories from the calculation, remove the code segment shown below.
elif entry.is_dir():
total += get_dir_size(entry.path)
Passing a file path to the function results in an error. To handle both file and directory paths without error, define a wrapper function as follows.
def get_size(path='.'):
if os.path.isfile(path):
return os.path.getsize(path)
elif os.path.isdir(path):
return get_dir_size(path)
print(get_size('data/src'))
# 56130856
print(get_size('data/src/lena_square.png'))
# 473831
Get directory size with os.listdir()
(Python 3.4 or earlier)
For Python 3.4 or earlier, use os.listdir()
, since os.scandir()
is unavailable.
Define a function as follows.
import os
def get_dir_size_old(path='.'):
total = 0
for p in os.listdir(path):
full_path = os.path.join(path, p)
if os.path.isfile(full_path):
total += os.path.getsize(full_path)
elif os.path.isdir(full_path):
total += get_dir_size_old(full_path)
return total
print(get_dir_size_old('data/src'))
# 56130856
The basic concept is the same as the function using os.scandir()
.
os.listdir()
returns a list of file and directory names. Each name is joined with the parent directory path with os.path.join()
to make a full path.
When dealing with symbolic links, os.path.isfile()
and os.path.isdir()
check the linked entities themselves. To specifically ignore symbolic links, use os.path.islink()
, which returns True
for them, and combine it with other conditions.
To exclude subdirectories from the calculation, remove the code segment shown below.
elif os.path.isdir(full_path):
total += get_dir_size_old(full_path)
Passing a file path to the function results in an error. To handle both file and directory paths without error, define a wrapper function as follows.
def get_size_old(path='.'):
if os.path.isfile(path):
return os.path.getsize(path)
elif os.path.isdir(path):
return get_dir_size_old(path)
print(get_size_old('data/src'))
# 56130856
print(get_size_old('data/src/lena_square.png'))
# 473831