Coverage for pyguymer3/return_folder_size.py: 5%
22 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-08 18:47 +0000
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-08 18:47 +0000
1#!/usr/bin/env python3
3# Define function ...
4def return_folder_size(
5 path,
6 /,
7 *,
8 allowHidden = False,
9 debug = __debug__,
10 ensureNFC = True,
11 follow_symlinks = True,
12 return_symlinks = True,
13):
14 """Return the total size of all files in a directory.
16 This function returns the total size of all files recursively in a directory.
18 Parameters
19 ----------
20 path : str
21 the directory to search
22 allowHidden : bool, optional
23 allow hidden files
24 debug : bool, optional
25 print debug messages
26 ensureNFC : bool, optional
27 ensure that the Unicode encoding is NFC
28 follow_symlinks : bool, optional
29 follow symbolic links
30 return_symlinks : bool, optional
31 include symbolic links in the returned list
33 Returns
34 -------
35 ans : int
36 the total size
38 Notes
39 -----
40 Copyright 2017 Thomas Guymer [1]_
42 References
43 ----------
44 .. [1] PyGuymer3, https://github.com/Guymer/PyGuymer3
45 """
47 # Import standard modules ...
48 import os
50 # Import sub-functions ...
51 from .make_path_safe import make_path_safe
53 # Initialize total ...
54 size = 0 # [B]
56 # Check it exists ...
57 if os.path.exists(path):
58 # Open folder ...
59 with os.scandir(path) as scanObj:
60 # Loop over folder contents ...
61 for entry in scanObj:
62 # Check if the user wants to perform debugging ...
63 if debug:
64 # Test if this part is hidden and print the full path for
65 # identification ...
66 if not allowHidden and entry.name.startswith("."):
67 print(f"WARNING: \"{entry.path}\" is hidden")
69 # Test if this part is illegal and print the full path for
70 # identification ...
71 if not entry.name.startswith(".") and entry.name != make_path_safe(entry.name, allowHidden = allowHidden, ensureNFC = ensureNFC):
72 print(f"WARNING: \"{entry.path}\" is illegal")
74 # Check if it might need following ...
75 if entry.is_dir(follow_symlinks = follow_symlinks):
76 # Check that the directory is list-able ...
77 # NOTE: On 20/Aug/2022 this was (incorrectly, in my opinion)
78 # returning False on regular folders on FreeBSD (but
79 # not MacOS) when passed "follow_symlinks = False".
80 if os.access(entry, os.X_OK):
81 # Recursively run this function again and add to the
82 # total ...
83 size += return_folder_size(
84 entry.path,
85 allowHidden = allowHidden,
86 debug = debug,
87 ensureNFC = ensureNFC,
88 follow_symlinks = follow_symlinks,
89 return_symlinks = return_symlinks,
90 ) # [B]
91 elif debug:
92 print(f"WARNING: \"{entry.path}\" cannot be listed")
94 # Check if it might need adding to the total ...
95 if entry.is_file(follow_symlinks = return_symlinks):
96 # Add to the total ...
97 size += os.path.getsize(entry) # [B]
98 elif debug:
99 print(f"WARNING: \"{path}\" does not exist")
101 # Return total ...
102 return size