Serial
Serial

Reputation: 8045

Saving Files In New Directory (python)

I'm trying to take the input file and save it into a new folder on my computer, but I can't figure out how to do it correctly.

Here is the code I tried:

from os.path import join as pjoin
a = raw_input("File Name: ")
filepath = "C:\Documents and Settings\User\My Documents\'a'"
fout = open(filepath, "w")
path_to_file = pjoin("C:\Documents and Settings User\My Documents\Dropbox",'a')
FILE = open(path_to_file, "w")

When I run it, it's putting two \ in between each sub-directory instead of one and it's telling me it's not an existing file or directory.

I am sure there is an easier way to do this, please help.

Upvotes: 2

Views: 8224

Answers (1)

Soham Chowdhury
Soham Chowdhury

Reputation: 2265

Why do you have unescaped "'quotes_like_this_inside_quotes'"? That may be a reason for that failure.

From what I can understand, the directories you are saving to are "C:\Documents and Settings\User\My Documents\' and 'C:\Documents and Settings\User\My Documents\'.

Whenever you are messing with directories/paths ALWAYS use os.expanduser('~/something/blah').

Try this:

from os.path import expanduser, join

path_to_file1 = join(expanduser('~/Dropbox/'), 'a')
path_to_file2 = join(expanduser('~'), 'a')
fout = open(path_to_file2, "w")
FILE = open(path_to_file1, "w")

And the double-backslashes are OK, AFAIK. Let me know if this works - I'm not on a Windows box at the moment.

Upvotes: 3

Related Questions