-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create the directory automatically if it doesn't exist #70
Labels
good first issue
Good for newcomers
Comments
hi @visch can i get this issue? |
@HyoungSooo sure! Be sure to think about nested directories, I think Python has some nice functions for this! |
HyoungSooo
added a commit
to HyoungSooo/target-csv
that referenced
this issue
Feb 16, 2024
HyoungSooo
added a commit
to HyoungSooo/target-csv
that referenced
this issue
Feb 16, 2024
edgarrmondragon
added a commit
that referenced
this issue
Feb 19, 2024
Create PR for #70 This PR is add decorator and test code for create new folder if it dose not exist. ```python def create_folder_if_not_exists(func: Any) -> Callable[..., int]: """Decorator to create folder if it does not exist.""" def wrapper(*args: Any, **kwargs: Any) -> int: try: filepath = kwargs["filepath"] except KeyError: filepath = args[0] folder = os.path.dirname(filepath) if not os.path.exists(folder) and folder != "": os.makedirs(folder) return func(*args, **kwargs) return wrapper ``` The reason why the try except statement is used is because other methods that call the write_csv function sometimes pass the filepath into args and sometimes kwarg. usage ```python @create_folder_if_not_exists def write_csv(filepath: Path, records: List[dict], schema: dict, **kwargs: Any) -> int: """Write a CSV file.""" if "properties" not in schema: raise ValueError("Stream's schema has no properties defined.") keys: List[str] = list(schema["properties"].keys()) with open(filepath, "w", encoding="utf-8", newline="") as fp: writer = csv.DictWriter(fp, fieldnames=keys, dialect="excel", **kwargs) writer.writeheader() for record_count, record in enumerate(records, start=1): writer.writerow(record) return record_count ``` If there is a better way please let me know --------- Co-authored-by: Edgar Ramírez Mondragón <[email protected]>
Closed by #73 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Create the directory automatically if it doesn't exist
The text was updated successfully, but these errors were encountered: