2

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.

3
  • The first of the many problems with your code is that backslashes in regular Python string need to be doubled because normally it's an special "escape" character. Alternatively, you can prefix strings with an r and then you don't have to do this. i.e. either "C:\\Documents and Settings\\User\\My Documents" or r"C:\Documents and Settings\User\My Documents". Also you need to use pjoin() to concatenate the directory name to a for both the input and output files you open(), and one of them should be opened for reading and the other for writing. Commented Apr 20, 2013 at 2:32
  • If you are calling your variable FILE so as not to shadow the builtin, just note that accepted convention for that is a single trailing underscore ie. file_, all caps are used for constants usually Commented Apr 20, 2013 at 3:43
  • @jamylak you're right. Commented Apr 20, 2013 at 6:58

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.