I/O Utils
clean_dir(path)
Recursively delete a directory if it exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
The directory path to remove. |
required |
Source code in gdutils/utils/io.py
52 53 54 55 56 57 58 59 60 | |
copy_file(src, dst)
Copy a single file to a destination path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src
|
str | Path
|
Source file path. |
required |
dst
|
str | Path
|
Destination file path. |
required |
Source code in gdutils/utils/io.py
180 181 182 183 184 185 186 187 188 | |
copy_files(pattern, dest)
Copy files matching a glob pattern to a destination directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
Glob pattern for source files. |
required |
dest
|
str | Path
|
Destination directory. |
required |
Source code in gdutils/utils/io.py
167 168 169 170 171 172 173 174 175 176 177 | |
dump_json(filename, data, **kwargs)
Write data to a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str | Path
|
The output file path. |
required |
data
|
dict
|
The serializable data to write. |
required |
**kwargs
|
Any
|
Additional arguments passed to |
{}
|
Source code in gdutils/utils/io.py
68 69 70 71 72 73 74 75 76 77 78 79 | |
dump_str(filename, data)
Dump string representation of data to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str | Path
|
Output file path. |
required |
data
|
Any
|
Data to write (converted to string). |
required |
Source code in gdutils/utils/io.py
206 207 208 209 210 211 212 213 214 215 | |
fPath(filepath, *path_parts, mkdir=False)
Construct a path relative to the parent directory of a given file path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str | Path
|
The reference file path (e.g. |
required |
*path_parts
|
str
|
Additional path components to join, usually just 'out' |
()
|
mkdir
|
bool
|
If True, create the resulting directory (including parents) if it doesn't exist. |
False
|
Returns:
| Type | Description |
|---|---|
Path
|
The resolved Path object. |
Source code in gdutils/utils/io.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |
get_iterable(x)
Ensure x is iterable (and treat string as non-iterable single item).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
The item to check. |
required |
Returns:
| Type | Description |
|---|---|
Iterable
|
An iterable containing x, or x itself if it is already an iterable (but not a str). |
Source code in gdutils/utils/io.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | |
get_timestamp(fmt='%d%m%Y_%H%M%S')
Return the current timestamp formatted as a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fmt
|
str
|
Format string (default: "%d%m%Y_%H%M%S"). |
'%d%m%Y_%H%M%S'
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted timestamp string. |
Source code in gdutils/utils/io.py
237 238 239 240 241 242 243 244 245 246 247 | |
greedy_download(*fnames, force=False)
Check if any of the given files are missing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*fnames
|
str | Path
|
Paths to check. |
()
|
force
|
bool
|
If True, always returns True. |
False
|
Returns:
| Type | Description |
|---|---|
bool
|
True if |
Source code in gdutils/utils/io.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | |
load_json(filename, **kwargs)
Read data from a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str | Path
|
The input file path. |
required |
**kwargs
|
Any
|
Additional arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
dict
|
The parsed JSON data. |
Source code in gdutils/utils/io.py
82 83 84 85 86 87 88 89 90 91 92 93 94 | |
load_str(filename, method='read')
Load a file content to string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str | Path
|
Path to the file. |
required |
method
|
str
|
Method to call on the file object (default: "read"). |
'read'
|
Returns:
| Type | Description |
|---|---|
str
|
The content of the file. |
Source code in gdutils/utils/io.py
191 192 193 194 195 196 197 198 199 200 201 202 203 | |
move_files(pattern, dest)
Move files matching a glob pattern to a destination directory. Overwrites existing files at the destination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
Glob pattern for source files. |
required |
dest
|
str | Path
|
Destination directory. |
required |
Source code in gdutils/utils/io.py
152 153 154 155 156 157 158 159 160 161 162 163 164 | |
read_env_path(env_var, default=None)
Read a path from an environment variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env_var
|
str
|
The name of the environment variable. |
required |
default
|
str | Path | None
|
Default value if the environment variable is not set. |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
The path from the environment variable as a Path object. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the environment variable is not set and no default is provided. |
Source code in gdutils/utils/io.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
remove_files(*patterns)
Remove files matching the given glob patterns if they exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*patterns
|
str
|
Glob patterns of files to remove. |
()
|
Source code in gdutils/utils/io.py
140 141 142 143 144 145 146 147 148 149 | |
remove_if_exists(fname)
Remove a file if it exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fname
|
str | Path
|
Path to the file to remove. |
required |
Source code in gdutils/utils/io.py
129 130 131 132 133 134 135 136 137 | |