pantank14
pantank14

Reputation: 175

How to pickle to a different folder on Linux?

When I used Windows, this was my code:

save_documents = open("pickled_algos/documents.pickle", "wb")
pickle.dump(documents, save_documents)
save_documents.close()

And what it did is make a second folder called "pickled_algos" and saved it there. Now I'm using Ubuntu Linux and when I write that same code I get this error:

Traceback (most recent call last):
File "PATH/sentimentProjectSaving.py", line 136, in <module>
save_documents = open("pickled_algos/documents.pickle", "wb")
FileNotFoundError: [Errno 2] No such file or directory: 'pickled_algos/documents.pickle'

How do I do the same thing I did in Windows, but on Linux?

EDIT: When I remove "pickled_algos/" then it works, the again the problem is that is saves into the same folder where the python file is.

Upvotes: 2

Views: 349

Answers (1)

nathancy
nathancy

Reputation: 46620

One reason could be because you're using a relative path instead of an absolute path. Specifically, your error mentions that the directory can't be found so you should first check if the directory exists.

import os

path = 'your path'

# Create target Directory if doesn't exist
if not os.path.exists(path):
    os.makedirs(path)
    print("Directory created ")
else:    
    print("Directory already exists")

Upvotes: 2

Related Questions