Spaces:
Sleeping
Sleeping
File size: 3,156 Bytes
d8d14f1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 70 71 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 |
# In order to accelerate the ops of creating files, we use the async file creation method.
import os
import asyncio
from aiofiles import open as aio_open
from typing import List
async def async_create_file(file_path: str, content: str) -> None:
"""
Asynchronously creates a file at the specified path and writes the given content to it.
Args:
file_path (str): The path where the file will be created.
content (str): The content to be written to the file.
Returns:
None
"""
async with aio_open(file_path, "w") as file:
await file.write(content)
async def create_multiple_files(
file_paths: List[str], contents: List[str]
) -> None:
"""
Asynchronously creates multiple files at the specified paths and writes the corresponding content to each file.
Args:
file_paths (List[str]): A list of paths where the files will be created.
contents (List[str]): A list of content to be written to each file, corresponding to the file paths.
Returns:
None
"""
tasks = [
async_create_file(file_path, content)
for file_path, content in zip(file_paths, contents)
]
await asyncio.gather(*tasks)
async def create_file_with_directory(
file_path: str, content: str
) -> None:
"""
Creates a file with the specified directory path and content. If the directory does not exist, it is created.
Args:
file_path (str): The path of the file to be created, including the directory.
content (str): The content to be written to the file.
Returns:
None
"""
directory = os.path.dirname(file_path)
if not os.path.exists(directory):
os.makedirs(directory)
await async_create_file(file_path, content)
def sync_create_file(file_path: str, content: str) -> None:
"""
Synchronously creates a file at the specified path and writes the given content to it.
Args:
file_path (str): The path where the file will be created.
content (str): The content to be written to the file.
Returns:
None
"""
asyncio.run(async_create_file(file_path, content))
def sync_create_multiple_files(
file_paths: List[str], contents: List[str]
) -> None:
"""
Synchronously creates multiple files at the specified paths and writes the corresponding content to each file.
Args:
file_paths (List[str]): A list of paths where the files will be created.
contents (List[str]): A list of content to be written to each file, corresponding to the file paths.
Returns:
None
"""
asyncio.run(create_multiple_files(file_paths, contents))
def sync_create_file_with_directory(
file_path: str, content: str
) -> None:
"""
Synchronously creates a file with the specified directory path and content. If the directory does not exist, it is created.
Args:
file_path (str): The path of the file to be created, including the directory.
content (str): The content to be written to the file.
Returns:
None
"""
asyncio.run(create_file_with_directory(file_path, content))
|