25 lines
597 B
Python
25 lines
597 B
Python
import aiofiles.os
|
|
import aiofiles
|
|
import os
|
|
|
|
async def read_file(path: str, callback=None):
|
|
if not await aiofiles.os.path.exists(path):
|
|
return None
|
|
|
|
async with aiofiles.open(path, "r") as f:
|
|
if callback:
|
|
return await callback(f)
|
|
return await f.read()
|
|
|
|
|
|
async def save_file(path: str, data, callback=None, *args, **kwargs):
|
|
dir_path = os.path.dirname(path)
|
|
if dir_path:
|
|
await aiofiles.os.makedirs(dir_path, exist_ok=True)
|
|
|
|
async with aiofiles.open(path, "w") as f:
|
|
if callback:
|
|
await callback(data, f, *args, **kwargs)
|
|
else:
|
|
await f.write(data)
|