Skip to content
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

Closed
visch opened this issue Feb 13, 2024 · 3 comments
Closed

Create the directory automatically if it doesn't exist #70

visch opened this issue Feb 13, 2024 · 3 comments
Labels
good first issue Good for newcomers

Comments

@visch
Copy link
Member

visch commented Feb 13, 2024

Create the directory automatically if it doesn't exist

@HyoungSooo
Copy link
Contributor

hi @visch can i get this issue?

@visch
Copy link
Member Author

visch commented Feb 15, 2024

@HyoungSooo sure! Be sure to think about nested directories, I think Python has some nice functions for this! Path.mkdir if I remember correctly

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]>
@edgarrmondragon
Copy link
Member

Closed by #73

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
Archived in project
Development

No branches or pull requests

3 participants