42

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?

2
  • 8
    There is just a single backslash. You're seeing the string representation. The file doesn't exist. Aug 12, 2012 at 18:36
  • 3
    The double backslash is not wrong, python prints/represents it that to the user way. 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'.
    – aneroid
    Aug 12, 2012 at 18:37

5 Answers 5

28

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__.

6
  • there's no d: 'C:\\Users\\user\\Desktop\\File_055123.pdf' Aug 12, 2012 at 19:00
  • Probably been edited out now.
    – aneroid
    Aug 13, 2012 at 16:49
  • 40
    Sorry but this was not a helpful answer. So if double slashes are the case, then how does one fix it????
    – DeeWBee
    Jul 6, 2016 at 19:34
  • @DeeWBee added some clarification about escape sequences and str vs repr.
    – aneroid
    Nov 21, 2018 at 0:09
  • 2
    @DeeWBee: You don't. There is nothing to fix. It's already fine. Nov 21, 2018 at 0:11
12

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'
1
  • In above example, if you display strs like >>> strs you should get c:\\desktop\\notebook. print doesnt show the escaped i.e. double slash \\
    – user966588
    Jul 8, 2015 at 10:24
4

save yourself from getting a headache you can use other slashes as well. if you know what I saying. the opposite looking slashes.

you're using now PDF = 'C:\Users\user\Desktop\File_%s.pdf' %item

try to use **

PDF = 'C:/Users/user/Desktop/File_%s.pdf' %item

** it won't be treated as a escaping character .

3

It doesn't. Double backslash is just the way of the computer of saying backslash. Yes, I know this sounds weird, but think of it this way - in order to represent special characters, backslash was chosen as an escaping character (e.g. \n means newline, and not the backslash character followed by the n character). But what happens if you actually want to print (or use) a backslash (possibly followed by more characters), but you don't want the computer to treat it as an escaping character? In that case we escape the backslash itself, meaning we use a double backslash so the computer will understand it's a single backslash.

It's done automatically in your case because of the r you added before the string.

-1

alwbtc @ I dare say: "I found the bug..."

replace

PDF = r'C:\Users\user\Desktop\File_%s.pdf' %item
doIt(PDF)`

with

for item in my_dictionary:
    PDF = r'C:\Users\user\Desktop\File_%s.pdf' % mydictionary[item]
    doIt(PDF)`

in fact you were really looking for File_pencil.pdf (not File_055123.pdf). You were sliding the index dictionary not its contents. This forum topic maybe a side-effect.

Not the answer you're looking for? Browse other questions tagged or ask your own question.