views:

79

answers:

4

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)

A: 

I'm going to have to say that your question is not very clear. Please explain better.

AJ00200
Ok! I'm going to edit my question!
Pol
+1  A: 

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.

Gabriel
+5  A: 

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.

Tim Pietzcker
Ye really the problem was in path. I made mistake. Wery big thanks for all.
Pol
Now please accept an answer, your accept rate is currently zero.
Philipp
A: 

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.

d.m