Skip to content

Reference

dsg_lib.common_functions.async_file_functions

async_file_functions.py

Async twin of file_functions.py. Every function here has the identical name and signature as its synchronous counterpart in dsg_lib.common_functions.file_functions, and simply runs that sync function in a worker thread via asyncio.to_thread so it can be awaited from async code (e.g. a FastAPI handler) without blocking the event loop.

This module intentionally contains no independent logic: validation, path resolution, and exception types (TypeError/ValueError/ FileNotFoundError) all live in file_functions.py, which remains the single source of truth. See that module's docstrings for full parameter and exception documentation -- the docstrings here are deliberately short, to avoid two divergent copies of the same prose.

Functions (identical names/signatures to file_functions.py, all async): delete_file, save_json, open_json, save_csv, append_csv, open_csv, save_text, open_text

Example:

import asyncio
from dsg_lib.common_functions import async_file_functions

async def main():
    await async_file_functions.save_json("test.json", {"key": "value"})
    data = await async_file_functions.open_json("test.json")
    print(data)

asyncio.run(main())

Author: Mike Ryan Date: 2026/07/22 License: MIT

append_csv(file_name, data, root_folder=None, delimiter=',', quotechar='"', columns=None) async

Async equivalent of file_functions.append_csv. Runs the sync call in a worker thread via asyncio.to_thread, so it is safe to await from an event loop without blocking it.

See file_functions.append_csv for full parameter and exception documentation.

Parameters:

Name Type Description Default
file_name str

Name of the CSV file to append to.

required
data list

Rows to append. Header row required unless columns is given.

required
root_folder str

Directory the file lives in. Defaults to "data/csv".

None
delimiter str

Field separator. Defaults to ",".

','
quotechar str

Quote character. Defaults to '"'.

'"'
columns List[str]

Column names for data's rows when data has no header row. Defaults to None.

None

Returns:

Name Type Description
str str

Same success message as file_functions.append_csv.

Raises:

Type Description
TypeError

Same conditions as file_functions.append_csv.

ValueError

Same conditions as file_functions.append_csv.

FileNotFoundError

Same conditions as file_functions.append_csv.

Source code in dsg_lib/common_functions/async_file_functions.py
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
async def append_csv(
    file_name: str,
    data: list,
    root_folder: str = None,
    delimiter: str = ",",
    quotechar: str = '"',
    columns: Optional[List[str]] = None,
) -> str:
    """
    Async equivalent of `file_functions.append_csv`. Runs the sync call in
    a worker thread via `asyncio.to_thread`, so it is safe to `await` from
    an event loop without blocking it.

    See `file_functions.append_csv` for full parameter and exception
    documentation.

    Args:
        file_name (str): Name of the CSV file to append to.
        data (list): Rows to append. Header row required unless `columns`
            is given.
        root_folder (str, optional): Directory the file lives in. Defaults
            to "data/csv".
        delimiter (str, optional): Field separator. Defaults to ",".
        quotechar (str, optional): Quote character. Defaults to '"'.
        columns (List[str], optional): Column names for `data`'s rows when
            `data` has no header row. Defaults to None.

    Returns:
        str: Same success message as `file_functions.append_csv`.

    Raises:
        TypeError: Same conditions as `file_functions.append_csv`.
        ValueError: Same conditions as `file_functions.append_csv`.
        FileNotFoundError: Same conditions as `file_functions.append_csv`.
    """
    return await asyncio.to_thread(
        file_functions.append_csv,
        file_name,
        data,
        root_folder=root_folder,
        delimiter=delimiter,
        quotechar=quotechar,
        columns=columns,
    )

delete_file(file_name, root_folder=None) async

Async equivalent of file_functions.delete_file. Runs the sync call in a worker thread via asyncio.to_thread, so it is safe to await from an event loop without blocking it.

See file_functions.delete_file for full parameter and exception documentation.

Parameters:

Name Type Description Default
file_name str

Name of the file to delete.

required
root_folder str

Directory the file lives in. Defaults to the matching "data/" directory.

None

Returns:

Name Type Description
str str

Same success message as file_functions.delete_file.

Raises:

Type Description
TypeError

Same conditions as file_functions.delete_file.

ValueError

Same conditions as file_functions.delete_file.

FileNotFoundError

Same conditions as file_functions.delete_file.

Source code in dsg_lib/common_functions/async_file_functions.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
async def delete_file(file_name: str, root_folder: str = None) -> str:
    """
    Async equivalent of `file_functions.delete_file`. Runs the sync call in
    a worker thread via `asyncio.to_thread`, so it is safe to `await` from
    an event loop without blocking it.

    See `file_functions.delete_file` for full parameter and exception
    documentation.

    Args:
        file_name (str): Name of the file to delete.
        root_folder (str, optional): Directory the file lives in. Defaults
            to the matching "data/<type>" directory.

    Returns:
        str: Same success message as `file_functions.delete_file`.

    Raises:
        TypeError: Same conditions as `file_functions.delete_file`.
        ValueError: Same conditions as `file_functions.delete_file`.
        FileNotFoundError: Same conditions as `file_functions.delete_file`.
    """
    return await asyncio.to_thread(
        file_functions.delete_file, file_name, root_folder=root_folder
    )

open_csv(file_name, delimiter=',', quote_level='minimal', skip_initial_space=True, root_folder=None, quotechar=None) async

Async equivalent of file_functions.open_csv. Runs the sync call in a worker thread via asyncio.to_thread, so it is safe to await from an event loop without blocking it.

See file_functions.open_csv for full parameter and exception documentation.

Parameters:

Name Type Description Default
file_name str

Name of the CSV file to open.

required
delimiter str

Field separator. Defaults to ",".

','
quote_level str

One of "none"/"minimal"/"all". Defaults to "minimal".

'minimal'
skip_initial_space bool

Defaults to True.

True
root_folder str

Directory the file lives in. Defaults to "data/csv".

None
quotechar str

Not supported -- passing anything other than None raises TypeError. Defaults to None.

None

Returns:

Name Type Description
list list

Same contents as file_functions.open_csv.

Raises:

Type Description
TypeError

Same conditions as file_functions.open_csv.

ValueError

Same conditions as file_functions.open_csv.

FileNotFoundError

Same conditions as file_functions.open_csv.

Source code in dsg_lib/common_functions/async_file_functions.py
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
async def open_csv(
    file_name: str,
    delimiter: str = ",",
    quote_level: str = "minimal",
    skip_initial_space: bool = True,
    root_folder: str = None,
    quotechar: str = None,
) -> list:
    """
    Async equivalent of `file_functions.open_csv`. Runs the sync call in a
    worker thread via `asyncio.to_thread`, so it is safe to `await` from an
    event loop without blocking it.

    See `file_functions.open_csv` for full parameter and exception
    documentation.

    Args:
        file_name (str): Name of the CSV file to open.
        delimiter (str, optional): Field separator. Defaults to ",".
        quote_level (str, optional): One of "none"/"minimal"/"all".
            Defaults to "minimal".
        skip_initial_space (bool, optional): Defaults to True.
        root_folder (str, optional): Directory the file lives in. Defaults
            to "data/csv".
        quotechar (str, optional): Not supported -- passing anything other
            than None raises `TypeError`. Defaults to None.

    Returns:
        list: Same contents as `file_functions.open_csv`.

    Raises:
        TypeError: Same conditions as `file_functions.open_csv`.
        ValueError: Same conditions as `file_functions.open_csv`.
        FileNotFoundError: Same conditions as `file_functions.open_csv`.
    """
    return await asyncio.to_thread(
        file_functions.open_csv,
        file_name,
        delimiter=delimiter,
        quote_level=quote_level,
        skip_initial_space=skip_initial_space,
        root_folder=root_folder,
        quotechar=quotechar,
    )

open_json(file_name, root_folder=None) async

Async equivalent of file_functions.open_json. Runs the sync call in a worker thread via asyncio.to_thread, so it is safe to await from an event loop without blocking it.

See file_functions.open_json for full parameter and exception documentation.

Parameters:

Name Type Description Default
file_name str

Name of the JSON file to open.

required
root_folder str

Directory the file lives in. Defaults to "data/json".

None

Returns:

Name Type Description
dict dict

Same contents as file_functions.open_json.

Raises:

Type Description
TypeError

Same conditions as file_functions.open_json.

FileNotFoundError

Same conditions as file_functions.open_json.

Source code in dsg_lib/common_functions/async_file_functions.py
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
async def open_json(file_name: str, root_folder: str = None) -> dict:
    """
    Async equivalent of `file_functions.open_json`. Runs the sync call in a
    worker thread via `asyncio.to_thread`, so it is safe to `await` from an
    event loop without blocking it.

    See `file_functions.open_json` for full parameter and exception
    documentation.

    Args:
        file_name (str): Name of the JSON file to open.
        root_folder (str, optional): Directory the file lives in. Defaults
            to "data/json".

    Returns:
        dict: Same contents as `file_functions.open_json`.

    Raises:
        TypeError: Same conditions as `file_functions.open_json`.
        FileNotFoundError: Same conditions as `file_functions.open_json`.
    """
    return await asyncio.to_thread(
        file_functions.open_json, file_name, root_folder=root_folder
    )

open_text(file_name, root_folder=None) async

Async equivalent of file_functions.open_text. Runs the sync call in a worker thread via asyncio.to_thread, so it is safe to await from an event loop without blocking it.

See file_functions.open_text for full parameter and exception documentation.

Parameters:

Name Type Description Default
file_name str

Name of the text file to open.

required
root_folder str

Directory the file lives in. Defaults to "data/text".

None

Returns:

Name Type Description
str str

Same contents as file_functions.open_text.

Raises:

Type Description
TypeError

Same conditions as file_functions.open_text.

FileNotFoundError

Same conditions as file_functions.open_text.

Source code in dsg_lib/common_functions/async_file_functions.py
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
async def open_text(file_name: str, root_folder: str = None) -> str:
    """
    Async equivalent of `file_functions.open_text`. Runs the sync call in a
    worker thread via `asyncio.to_thread`, so it is safe to `await` from an
    event loop without blocking it.

    See `file_functions.open_text` for full parameter and exception
    documentation.

    Args:
        file_name (str): Name of the text file to open.
        root_folder (str, optional): Directory the file lives in. Defaults
            to "data/text".

    Returns:
        str: Same contents as `file_functions.open_text`.

    Raises:
        TypeError: Same conditions as `file_functions.open_text`.
        FileNotFoundError: Same conditions as `file_functions.open_text`.
    """
    return await asyncio.to_thread(
        file_functions.open_text, file_name, root_folder=root_folder
    )

save_csv(file_name, data, root_folder=None, delimiter=',', quotechar='"') async

Async equivalent of file_functions.save_csv. Runs the sync call in a worker thread via asyncio.to_thread, so it is safe to await from an event loop without blocking it.

See file_functions.save_csv for full parameter and exception documentation.

Parameters:

Name Type Description Default
file_name str

Name of the CSV file to save (".csv" is appended if missing).

required
data list

Rows to save, including the header as the first row.

required
root_folder str

Directory to save into. Defaults to "data/csv".

None
delimiter str

Field separator. Defaults to ",".

','
quotechar str

Quote character. Defaults to '"'.

'"'

Returns:

Name Type Description
str str

Same success message as file_functions.save_csv.

Raises:

Type Description
TypeError

Same conditions as file_functions.save_csv.

ValueError

Same conditions as file_functions.save_csv.

Source code in dsg_lib/common_functions/async_file_functions.py
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
async def save_csv(
    file_name: str,
    data: list,
    root_folder: str = None,
    delimiter: str = ",",
    quotechar: str = '"',
) -> str:
    """
    Async equivalent of `file_functions.save_csv`. Runs the sync call in a
    worker thread via `asyncio.to_thread`, so it is safe to `await` from an
    event loop without blocking it.

    See `file_functions.save_csv` for full parameter and exception
    documentation.

    Args:
        file_name (str): Name of the CSV file to save (".csv" is appended
            if missing).
        data (list): Rows to save, including the header as the first row.
        root_folder (str, optional): Directory to save into. Defaults to
            "data/csv".
        delimiter (str, optional): Field separator. Defaults to ",".
        quotechar (str, optional): Quote character. Defaults to '"'.

    Returns:
        str: Same success message as `file_functions.save_csv`.

    Raises:
        TypeError: Same conditions as `file_functions.save_csv`.
        ValueError: Same conditions as `file_functions.save_csv`.
    """
    return await asyncio.to_thread(
        file_functions.save_csv,
        file_name,
        data,
        root_folder=root_folder,
        delimiter=delimiter,
        quotechar=quotechar,
    )

save_json(file_name, data, root_folder=None, indent=None, ensure_ascii=True) async

Async equivalent of file_functions.save_json. Runs the sync call in a worker thread via asyncio.to_thread, so it is safe to await from an event loop without blocking it.

See file_functions.save_json for full parameter and exception documentation.

Parameters:

Name Type Description Default
file_name str

Name of the JSON file to save (".json" is appended if missing).

required
data list | dict

The data to serialize and save.

required
root_folder str

Directory to save into. Defaults to "data/json".

None
indent int

Indent width for pretty-printed output. Defaults to None (compact).

None
ensure_ascii bool

Escape non-ASCII characters as \uXXXX if True (default).

True

Returns:

Name Type Description
str str

Same success message as file_functions.save_json.

Raises:

Type Description
TypeError

Same conditions as file_functions.save_json.

ValueError

Same conditions as file_functions.save_json.

Source code in dsg_lib/common_functions/async_file_functions.py
 72
 73
 74
 75
 76
 77
 78
 79
 80
 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
async def save_json(
    file_name: str,
    data,
    root_folder: str = None,
    indent: int = None,
    ensure_ascii: bool = True,
) -> str:
    """
    Async equivalent of `file_functions.save_json`. Runs the sync call in a
    worker thread via `asyncio.to_thread`, so it is safe to `await` from an
    event loop without blocking it.

    See `file_functions.save_json` for full parameter and exception
    documentation.

    Args:
        file_name (str): Name of the JSON file to save (".json" is
            appended if missing).
        data (list | dict): The data to serialize and save.
        root_folder (str, optional): Directory to save into. Defaults to
            "data/json".
        indent (int, optional): Indent width for pretty-printed output.
            Defaults to None (compact).
        ensure_ascii (bool, optional): Escape non-ASCII characters as
            `\\uXXXX` if True (default).

    Returns:
        str: Same success message as `file_functions.save_json`.

    Raises:
        TypeError: Same conditions as `file_functions.save_json`.
        ValueError: Same conditions as `file_functions.save_json`.
    """
    return await asyncio.to_thread(
        file_functions.save_json,
        file_name,
        data,
        root_folder=root_folder,
        indent=indent,
        ensure_ascii=ensure_ascii,
    )

save_text(file_name, data, root_folder=None) async

Async equivalent of file_functions.save_text. Runs the sync call in a worker thread via asyncio.to_thread, so it is safe to await from an event loop without blocking it.

See file_functions.save_text for full parameter and exception documentation.

Parameters:

Name Type Description Default
file_name str

Name of the text file to save (".txt" is appended if missing).

required
data str

The text data to save.

required
root_folder str

Directory to save into. Defaults to "data/text".

None

Returns:

Name Type Description
str str

Same success message as file_functions.save_text.

Raises:

Type Description
TypeError

Same conditions as file_functions.save_text.

ValueError

Same conditions as file_functions.save_text.

Source code in dsg_lib/common_functions/async_file_functions.py
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
async def save_text(file_name: str, data: str, root_folder: str = None) -> str:
    """
    Async equivalent of `file_functions.save_text`. Runs the sync call in a
    worker thread via `asyncio.to_thread`, so it is safe to `await` from an
    event loop without blocking it.

    See `file_functions.save_text` for full parameter and exception
    documentation.

    Args:
        file_name (str): Name of the text file to save (".txt" is appended
            if missing).
        data (str): The text data to save.
        root_folder (str, optional): Directory to save into. Defaults to
            "data/text".

    Returns:
        str: Same success message as `file_functions.save_text`.

    Raises:
        TypeError: Same conditions as `file_functions.save_text`.
        ValueError: Same conditions as `file_functions.save_text`.
    """
    return await asyncio.to_thread(
        file_functions.save_text, file_name, data, root_folder=root_folder
    )