4

I am using Python 3.3 on Windows 7.

Here is the problem.

When I have a filename starting with a number it changes wrong.

For example:

>>> 'E:\DOCUMENTS\1.jpg'
'E:\\DOCUMENTS\x01.jpg'

I am aware that I can fix it manually by adding an escaping backslash.

>>> 'E:\DOCUMENTS\\1.jpg'
'E:\\DOCUMENTS\\1.jpg'

Or by adding "r" in front of the string.

>>> r'E:\DOCUMENTS\1.jpg'
'E:\\DOCUMENTS\\1.jpg'

But I cannot do it manually, because I don't know what the path will be.

What are the possible solutions?

UPDATE: As @Blender suggested, I was going to post the code. When I rewrote it, I realized that originally there was a mistake, that leaded me to a wrong conclusion. As far as I have understood, the described above situation, when it is necessary to make a string with a path raw dynamically does not happen. It can only happen when the path is written manually.

import os
from PIL import Image as PIL
from PIL import ImageTk

def searchforimages(dir):
    imagelist=[]
    for file in os.listdir(dir):
        fileabspath=os.path.join(dir,file)
        try:
            # the problem was here originally, but now it is ok.
            # since "fileabspath" get passes as a raw string,
            # so there is no problem for PIL.open() to open it
            PIL.open(fileabspath)
            imagelist.append(fileabspath)
        except:
            continue
    return imagelist

searchforimages('E:\photos')

#the problem only happens, when path is written manually
path='E:\photos\1.jpg'
PIL.open(path)

So now I just want to confirm, the problem when it is necessary to make a string with a path raw dynamically never really happens, does it?

Sashko Kopyl
  • 71
  • 1
  • 1
  • 8
  • 4
    It's not a "fix": this is literally how the string is represented. `\1` is `\x01`. Just out of curiosity, how does this problem even arise? Why does it matter that you don't know what the path is beforehand? – Blender Dec 21 '13 at 07:32
  • I want to open images with PIL module and those files, which names start with digits, I can't open. – Sashko Kopyl Dec 21 '13 at 08:17
  • That doesn't make sense. How are you getting these names? – Blender Dec 21 '13 at 09:38
  • I use os.listdir and os.path.abspath. – Sashko Kopyl Dec 21 '13 at 10:05
  • If there is something like 'E:\DOCUMENTS\1.jpg' I get such an error. File "C:\Python33\lib\site-packages\PIL\Image.py", line 1974, in open OSError: [Errno 22] Invalid argument: 'E:\\DOCUMENTS\x01.jpg' – Sashko Kopyl Dec 21 '13 at 10:12
  • Can you post your *exact* code (or the shortest example that reproduces the problem, without hard-coding the path)? This could very well be a bug in PIL. – Blender Dec 21 '13 at 10:18
  • >Blender I have added the code to the question and have found that I was wrong. It seems the problem described in the question never happens. – Sashko Kopyl Dec 21 '13 at 13:10

1 Answers1

1

\ only matters when it is used in string literal.

>>> path = input() # `a\n\1` in the following line is typed by me (user).
a\n\1
>>> path
'a\\n\\1'
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Specifically, it only has its special behavior when used in a string literal. When a backslash comes from anywhere else, that's just a real backslash, not the start of an escape sequence. It still matters in that it's a part of the string. – user2357112 Dec 21 '13 at 09:17