Skip to content

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
def clean_dir(path: str | Path):
    """
    Recursively delete a directory if it exists.

    Args:
        path: The directory path to remove.
    """
    if Path(path).exists():
        shutil.rmtree(str(path))

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
def copy_file(src: str | Path, dst: str | Path) -> None:
    """
    Copy a single file to a destination path.

    Args:
        src: Source file path.
        dst: Destination file path.
    """
    shutil.copyfile(str(src), str(dst))

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
def copy_files(pattern: str, dest: str | Path) -> None:
    """
    Copy files matching a glob pattern to a destination directory.

    Args:
        pattern: Glob pattern for source files.
        dest: Destination directory.
    """
    dest_path = Path(dest).absolute()
    for f in glob.glob(str(pattern)):
        shutil.copyfile(f, dest_path / os.path.basename(f))

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 json.dump. Defaults to indent=4.

{}
Source code in gdutils/utils/io.py
68
69
70
71
72
73
74
75
76
77
78
79
def dump_json(filename: str | Path, data: dict, **kwargs: Any):
    """
    Write data to a JSON file.

    Args:
        filename: The output file path.
        data: The serializable data to write.
        **kwargs: Additional arguments passed to `json.dump`. Defaults to `indent=4`.
    """
    kwargs.setdefault("indent", 4)
    with open(str(filename), "w") as f:
        json.dump(data, f, **kwargs)

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
def dump_str(filename: str | Path, data: Any) -> None:
    """
    Dump string representation of data to a file.

    Args:
        filename: Output file path.
        data: Data to write (converted to string).
    """
    with open(str(filename), "w") as f:
        f.write(str(data))

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. __file__).

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
def fPath(filepath: str | Path, *path_parts: str, mkdir: bool = False) -> Path:
    """
    Construct a path relative to the parent directory of a given file path.

    Args:
        filepath: The reference file path (e.g. `__file__`).
        *path_parts: Additional path components to join, usually just 'out'
        mkdir: If True, create the resulting directory (including parents) if it doesn't exist.

    Returns:
        The resolved Path object.
    """
    full_path = Path(filepath).parent.joinpath(*path_parts)
    if mkdir:
        full_path.mkdir(exist_ok=True, parents=True)
    return full_path

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
def get_iterable(x: Any) -> Iterable:
    """
    Ensure x is iterable (and treat string as non-iterable single item).

    Args:
        x: The item to check.

    Returns:
        An iterable containing x, or x itself if it is already an iterable (but not a str).
    """
    if isinstance(x, str):
        return (x,)
    elif isinstance(x, Iterable):
        return x
    else:
        return (x,)

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
def get_timestamp(fmt: str = "%d%m%Y_%H%M%S") -> str:
    """
    Return the current timestamp formatted as a string.

    Args:
        fmt: Format string (default: "%d%m%Y_%H%M%S").

    Returns:
        Formatted timestamp string.
    """
    return datetime.now().strftime(fmt)

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 force is set or if any file is missing, False otherwise.

Source code in gdutils/utils/io.py
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
def greedy_download(*fnames: str | Path, force: bool = False) -> bool:
    """
    Check if any of the given files are missing.

    Args:
        *fnames: Paths to check.
        force: If True, always returns True.

    Returns:
        True if `force` is set or if any file is missing, False otherwise.
    """
    if force:
        return True
    for x in fnames:
        if not os.path.isfile(x):
            return True
    return False

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 json.load.

{}

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
def load_json(filename: str | Path, **kwargs: Any) -> dict:
    """
    Read data from a JSON file.

    Args:
        filename: The input file path.
        **kwargs: Additional arguments passed to `json.load`.

    Returns:
        The parsed JSON data.
    """
    with open(str(filename), "r") as f:
        return json.load(f, **kwargs)

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
def load_str(filename: str | Path, method: str = "read") -> str:
    """
    Load a file content to string.

    Args:
        filename: Path to the file.
        method: Method to call on the file object (default: "read").

    Returns:
        The content of the file.
    """
    with open(str(filename), "r") as f:
        return getattr(f, method)()

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
def move_files(pattern: str, dest: str | Path) -> None:
    """
    Move files matching a glob pattern to a destination directory.
    Overwrites existing files at the destination.

    Args:
        pattern: Glob pattern for source files.
        dest: Destination directory.
    """
    for f in glob.glob(str(pattern)):
        dest_file = Path(dest) / os.path.basename(f)
        remove_if_exists(dest_file)
        shutil.move(f, str(dest))

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
def read_env_path(env_var: str, default: str | Path | None = None) -> Path:
    """
    Read a path from an environment variable.

    Args:
        env_var: The name of the environment variable.
        default: Default value if the environment variable is not set.

    Returns:
        The path from the environment variable as a Path object.

    Raises:
        ValueError: If the environment variable is not set and no default is provided.
    """
    val = os.getenv(env_var)
    if val is None:
        if default is not None:
            return Path(default)
        raise ValueError(f"Environment variable '{env_var}' not set.")
    return Path(val)

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
def remove_files(*patterns: str) -> None:
    """
    Remove files matching the given glob patterns if they exist.

    Args:
        *patterns: Glob patterns of files to remove.
    """
    for pattern in patterns:
        for f in glob.glob(str(pattern)):
            remove_if_exists(f)

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
def remove_if_exists(fname: str | Path) -> None:
    """
    Remove a file if it exists.

    Args:
        fname: Path to the file to remove.
    """
    if os.path.exists(fname):
        os.remove(fname)