operations
crawl_dir(target=None, filetype_filter=None, return_type='all')
Crawl a directory and return an object with all found files & dirs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target |
str | Path
|
The target directory to crawl |
None
|
filetype_filter |
str
|
An optional filetype filter str; only files matching this filter will be returned |
None
|
return_type |
str
|
Return |
'all'
|
Returns
(list[Path]): A list of `Path` objects if `return_type` is `dirs` or `files`
(dict[str, list[Path]]): If `return_type` is `all`, return a dict `{"file": [], "dirs": []}`
Raises
ValueError: When input validation fails
FileNotFoundError: When a file/directory path cannot be found
Source code in src/red_utils/std/path_utils/operations.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
delete_path(rm_path=None)
Recursively delete a path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rm_path |
str | Path
|
The path to delete |
None
|
Returns
(bool): `True` if `rm_path` deleted successfully
(bool): `False` if `rm_path` not deleted successfully
Raises
FileNotFoundError: When path to delete does not exist
PermissionError: When permission to delete the path is not granted
Exception: Generic `Exception` when operation fails and is not caught by another exception
Source code in src/red_utils/std/path_utils/operations.py
ensure_dirs_exist(ensure_dirs=None)
Loop over a list of directories and create any paths that do not already exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ensure_dirs |
list[str] | list[Path]
|
A list of directory paths formatted as strings or Path objects. |
None
|
Source code in src/red_utils/std/path_utils/operations.py
export_json(input=None, output_dir=JSON_DIR, output_filename=f'{file_ts()}_unnamed_json.json')
Export JSON object to an output file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input |
str | list[list, dict] | dict[str, Any]
|
The input object to be output to a file. |
None
|
output_dir |
str
|
The directory where a .json file will be saved. |
JSON_DIR
|
output_filename |
str
|
The name of the file that will be saved in output_dir. |
f'{file_ts()}_unnamed_json.json'
|
Raises
FileExistsError: When the output path already exists
FileNotFoundError: When the export path does not exist
Exception: When other exceptions have not been caught, a generic `Exception` is raised
Source code in src/red_utils/std/path_utils/operations.py
file_ts(fmt='%Y-%m-%d_%H:%M:%S')
Return a formatted timestamp, useful for prepending to dir/file names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fmt |
str
|
String that defines the format of a timestamp |
'%Y-%m-%d_%H:%M:%S'
|
Returns
(str): A formatted datetime string
Source code in src/red_utils/std/path_utils/operations.py
list_files(in_dir=None, ext_filter=None, return_files=[])
List all files in a path, optionally filtering by file extension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_dir |
str
|
Directory path to scan |
None
|
ext_filter |
str
|
Filetype to search for |
None
|
return_files |
list[Path]
|
Used by the function to recurse through subdirectories |
[]
|
Returns
(list[Path]): A list of found files, represented as `Path` objects