Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rid of double backslash in python windows file path string? [duplicate]

I have a dictionary:

my_dictionary = {"058498":"table", "064165":"pen", "055123":"pencil"} 

I iterate over it:

for item in my_dictionary:     PDF = r'C:\Users\user\Desktop\File_%s.pdf' %item     doIt(PDF)  def doIt(PDF):     part = MIMEBase('application', "octet-stream")     part.set_payload( open(PDF,"rb").read() ) 

But I get this error:

IOError: [Errno 2] No such file or directory: 'C:\\Users\\user\\Desktop\\File_055123.pdf' 

It can't find my file. Why does it think there are double backslashes in file path?

like image 257
alwbtc Avatar asked Aug 12 '12 18:08

alwbtc


People also ask

Why is Python adding backslashes to string?

In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return. Conversely, prefixing a special character with "\" turns it into an ordinary character.

What does 2 backslashes mean in Python?

Python has two division operators, a single slash character for classic division and a double-slash for “floor” division (rounds down to nearest whole number). Classic division means that if the operands are both integers, it will perform floor division, while for floating point numbers, it represents true division.


2 Answers

The double backslash is not wrong, python represents it way that to the user. In each double backslash \\, the first one escapes the second to imply an actual backslash. If a = r'raw s\tring' and b = 'raw s\\tring' (no 'r' and explicit double slash) then they are both represented as 'raw s\\tring'.

>>> a = r'raw s\tring' >>> b = 'raw s\\tring' >>> a 'raw s\\tring' >>> b 'raw s\\tring' 

For clarification, when you print the string, you'd see it as it would get used, like in a path - with just one backslash:

>>> print(a) raw s\tring >>> print(b) raw s\tring 

And in this printed string case, the \t doesn't imply a tab, it's a backslash \ followed by the letter 't'.

Otherwise, a string with no 'r' prefix and a single backslash would escape the character after it, making it evaluate the 't' following it == tab:

>>> t = 'not raw s\tring'  # here '\t' = tab >>> t 'not raw s\tring' >>> print(t)  # will print a tab (and no letter 't' in 's\tring') not raw s       ring 

So in the PDF path+name:

>>> item = 'xyz' >>> PDF = r'C:\Users\user\Desktop\File_%s.pdf' % item >>> PDF         # the representation of the string, also in error messages 'C:\\Users\\user\\Desktop\\File_xyz.pdf' >>> print(PDF)  # "as used" C:\Users\user\Desktop\File_xyz.pdf 

More info about escape sequences in the table here. Also see __str__ vs __repr__.

like image 69
aneroid Avatar answered Sep 18 '22 08:09

aneroid


Double backslashes are due to r, raw string:

r'C:\Users\user\Desktop\File_%s.pdf' , 

It is used because the \ might escape some of the characters.

>>> strs = "c:\desktop\notebook"  >>> print strs                #here print thinks that \n in \notebook is the newline char c:\desktop otebook  >>> strs = r"c:\desktop\notebook"  #using r'' escapes the \ >>> print strs  c:\desktop\notebook  >>> print repr(strs)   #actual content of strs 'c:\\desktop\\notebook' 
like image 20
Ashwini Chaudhary Avatar answered Sep 21 '22 08:09

Ashwini Chaudhary