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 |
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 |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Same success message as |
Raises:
| Type | Description |
|---|---|
TypeError
|
Same conditions as |
ValueError
|
Same conditions as |
FileNotFoundError
|
Same conditions as |
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 | |
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/ |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Same success message as |
Raises:
| Type | Description |
|---|---|
TypeError
|
Same conditions as |
ValueError
|
Same conditions as |
FileNotFoundError
|
Same conditions as |
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 | |
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 |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
Same contents as |
Raises:
| Type | Description |
|---|---|
TypeError
|
Same conditions as |
ValueError
|
Same conditions as |
FileNotFoundError
|
Same conditions as |
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 | |
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 |
Raises:
| Type | Description |
|---|---|
TypeError
|
Same conditions as |
FileNotFoundError
|
Same conditions as |
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 | |
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 |
Raises:
| Type | Description |
|---|---|
TypeError
|
Same conditions as |
FileNotFoundError
|
Same conditions as |
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 | |
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 |
Raises:
| Type | Description |
|---|---|
TypeError
|
Same conditions as |
ValueError
|
Same conditions as |
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 | |
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
|
True
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Same success message as |
Raises:
| Type | Description |
|---|---|
TypeError
|
Same conditions as |
ValueError
|
Same conditions as |
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 | |
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 |
Raises:
| Type | Description |
|---|---|
TypeError
|
Same conditions as |
ValueError
|
Same conditions as |
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 | |