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

FileExistsError when saving data to existing directory outside current path #71

Open
gfbarbieri opened this issue Feb 10, 2024 · 0 comments

Comments

@gfbarbieri
Copy link

I've encountered an issue with the save() method in the DataDict class, where it fails to properly handle saving data to a directory outside the current working directory. The result is FileExistsError because the folder data already exists one level above the program file location. Here is the code that caused the error:

results.save(exp_name='firm_dynamics', path=os.path.join('..','data'))

Looking into the .save() method, I believe the problematic code snippet is as follows:

# Create output directory if it doesn't exist
if path not in os.listdir():
    os.makedirs(path)

This logic checks if the path is in the current directory's listing (os.listdir()), leading to a FileExistsError when attempting to save data to an existing directory located outside the current directory, e.g., path='../'. A potential solution would be to directly check for the existence of the specified path and create it if it does not exist, avoiding the assumption that path should be within the current directory's listing:

if not os.path.exists(path):
    os.makedirs(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant