0

How can I add letter to path?

For example if i have a path like 'c:\example2\media\uploads\test5.txt' (stored in a variable), but I need something like r'c:\example2\media\uploads\test5.txt', how can I add letter `r?

Because the function open() does not want to open the first path. When I try add path to function open() it gives me an error and path like this: u'c:\\example2\\media\\uploads\\test5.txt' and says that file or directory is absent. What should i do?

Error looks like:

[Error 3] The system cannot find the path specified: u'C:\\example2\\media\\upload\\ZipFile.zip'

when i do this open('c:\example2\media\uploads\test5.txt') it not work. And gives me error ( which you can see on the top)

asked Jun 24, 2010 at 19:04

3 Answers 3

6

From the error message it is clear that the string is stored in the correct format (backslashes are escaped by doubling). So it seems the path is wrong, and the file is indeed absent.

On the other hand, in your second example that you added in your edit, you use open('c:\example2\media\uploads\test5.txt') - this will definitely fail because \t is a tab character (whereas all the other backslash-letter combinations don't exist, so the backslash will be treated like it was escaped correctly). But you said that the string was stored in a variable, so I don't see how this example helps here.

Consider the following:

>>> path = 'c:\example2\media\uploads\test5.txt'
>>> path
'c:\\example2\\media\\uploads\test5.txt'

See? All the backslashes are converted to escaped backslashes except for the \t one because that's the only one with special meaning. And now, of course, this path is wrong. So if the variables you're referring to have been defined this way (and now contain invalid paths) there's not much you can do except fix the source:

>>> path = r'c:\example2\media\uploads\test5.txt'
>>> path
'c:\\example2\\media\\uploads\\test5.txt'

You might think you could "fix" a defective path afterwards like this:

>>> path = 'c:\example2\media\uploads\test5.txt'
>>> path.replace("\t","\\t")
'c:\\example2\\media\\uploads\\test5.txt'

...but there are many other escape codes (\b, \r, \n etc.), so that's not really a feasible way, especially because you'd be doctoring around the symptoms instead of fixing the underlying problem.

answered Jun 24, 2010 at 19:20
Sign up to request clarification or add additional context in comments.

1 Comment

Ye really the problem was in path. I made mistake. Wery big thanks for all.
2

well is it upload or uploads? Your question says the one, but your error says the other. The 'u' at the beginning indicates that the string is unicode format, this shouldn't have any impact. the '\\' character is necessary for python to escape the '\' character.

answered Jun 24, 2010 at 19:19

Comments

0

The letter 'r' is for the Python interpreter only. It indicates, that interpreter shouldn't escape any sequences, when parsing a string. If the string is already stored in a variable, there is nothing to do with the 'r' letter.

I guess the problem is that there is actualy no such file. Try copying this line you get in the exception (the path from that line I mean) and pasting it into windows run dialog (Win+r will show it up). Then hit 'enter'.

If you get an error, check the paths. You have both upload and uploads in your question, make sure you use the right one in your code.

answered Jun 24, 2010 at 19:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.