get the total disk size of a given directory, optionally summing up recursively and limiting to a given filter list
(path: Union[str, Path], recursive: bool=True, pattern: Optional[str]=None)
| 142 | |
| 143 | @enforce_types |
| 144 | def get_dir_size(path: Union[str, Path], recursive: bool=True, pattern: Optional[str]=None) -> Tuple[int, int, int]: |
| 145 | """get the total disk size of a given directory, optionally summing up |
| 146 | recursively and limiting to a given filter list |
| 147 | """ |
| 148 | num_bytes, num_dirs, num_files = 0, 0, 0 |
| 149 | for entry in os.scandir(path): |
| 150 | if (pattern is not None) and (pattern not in entry.path): |
| 151 | continue |
| 152 | if entry.is_dir(follow_symlinks=False): |
| 153 | if not recursive: |
| 154 | continue |
| 155 | num_dirs += 1 |
| 156 | bytes_inside, dirs_inside, files_inside = get_dir_size(entry.path) |
| 157 | num_bytes += bytes_inside |
| 158 | num_dirs += dirs_inside |
| 159 | num_files += files_inside |
| 160 | else: |
| 161 | num_bytes += entry.stat(follow_symlinks=False).st_size |
| 162 | num_files += 1 |
| 163 | return num_bytes, num_dirs, num_files |
| 164 | |
| 165 | |
| 166 | CRON_COMMENT = 'archivebox_schedule' |
no outgoing calls
no test coverage detected