skip dir size calculation when path is too long
This commit is contained in:
parent
c94ed53570
commit
91c4641199
|
@ -146,20 +146,24 @@ def get_dir_size(path: Union[str, Path], recursive: bool=True, pattern: Optional
|
||||||
recursively and limiting to a given filter list
|
recursively and limiting to a given filter list
|
||||||
"""
|
"""
|
||||||
num_bytes, num_dirs, num_files = 0, 0, 0
|
num_bytes, num_dirs, num_files = 0, 0, 0
|
||||||
for entry in os.scandir(path):
|
try:
|
||||||
if (pattern is not None) and (pattern not in entry.path):
|
for entry in os.scandir(path):
|
||||||
continue
|
if (pattern is not None) and (pattern not in entry.path):
|
||||||
if entry.is_dir(follow_symlinks=False):
|
|
||||||
if not recursive:
|
|
||||||
continue
|
continue
|
||||||
num_dirs += 1
|
if entry.is_dir(follow_symlinks=False):
|
||||||
bytes_inside, dirs_inside, files_inside = get_dir_size(entry.path)
|
if not recursive:
|
||||||
num_bytes += bytes_inside
|
continue
|
||||||
num_dirs += dirs_inside
|
num_dirs += 1
|
||||||
num_files += files_inside
|
bytes_inside, dirs_inside, files_inside = get_dir_size(entry.path)
|
||||||
else:
|
num_bytes += bytes_inside
|
||||||
num_bytes += entry.stat(follow_symlinks=False).st_size
|
num_dirs += dirs_inside
|
||||||
num_files += 1
|
num_files += files_inside
|
||||||
|
else:
|
||||||
|
num_bytes += entry.stat(follow_symlinks=False).st_size
|
||||||
|
num_files += 1
|
||||||
|
except OSError:
|
||||||
|
# e.g. FileNameTooLong or other error while trying to read dir
|
||||||
|
pass
|
||||||
return num_bytes, num_dirs, num_files
|
return num_bytes, num_dirs, num_files
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue