1

I used os.join.path() to load image in a folder. But I found the function cannot give accurate path when it is used in defining another function in some cases. For example:

def Myfuncion(something)
    desiredPath = os.path.join('myPath','apple.jpeg')
    #desiredPath = os.path.normpath(os.path.join('myPath','apple.jpeg')) 

    print desiredPath
    return

When I implement the function, the printed result of the path is:

 myPath\apple.jpeg

It is illegal for image loading. But os.path.join() works well in Pythonconsole.

How to make the path generated in such function definition have double backslashes?

Also, it is noted os.path.normpath also cannot work well sometimes. For example:

 os.path.normpath('myPath\apple')

It should give the result:

 myPath\\apple

But instead, it results in:

 'myPath\x07pple'

How come??

falsetru
  • 357,413
  • 63
  • 732
  • 636
jwm
  • 4,832
  • 10
  • 46
  • 78
  • 2
    You are aware that a string defined with double backslashes (that's called escaped) will show with only one backslash when printed? And that `'\x07'` is equivalent to `'\a'`? – Klaus D. Nov 13 '16 at 04:45
  • Yes! I use print only for debugging, since I found the image at the path is not loaded properly. I used Image.open() to load the image, and then saved it to another folder – jwm Nov 13 '16 at 04:49
  • Please do independent research on how backslashes function in strings. – TigerhawkT3 Nov 13 '16 at 04:53
  • @TigerhawkT3 I got it. Thank you! – jwm Nov 13 '16 at 05:10

1 Answers1

2

\a is equivalent to \x07. (See escape sequence part in String and bytes literals.)

>>> '\a'
'\x07'

You need to escape \ to mean backslash literally:

>>> '\\a'
'\\a'
>>> print('\\a')
\a

or, use raw string literal:

>>> r'\a'
'\\a'
>>> print(r'\a')
\a
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • But the path with single backlishes is illegal for python to load an image. – jwm Nov 13 '16 at 04:46
  • @jingweimo, My point is that `os.path.join('myPath','apple.jpeg') == 'myPath\\apple.jpeg'` (in other word, `os.path.join('myPath','apple.jpeg') == 'myPath\apple.jpeg'` – falsetru Nov 13 '16 at 04:50
  • @jingweimo path with single backslash is legal but it makes problem when there are special chars like `\n`, `\t`, `\a`, `\u`, etc. So it is safer to use \\ or / (even on Windows) or `os.path.join()` – furas Nov 13 '16 at 04:51
  • @jingweimo, Please check escape sequence part of documentation. I provided the link to the documentation in the answer. – falsetru Nov 13 '16 at 04:51
  • Thanks! How to deal with that case of special chars? – jwm Nov 13 '16 at 04:54
  • @jingweimo, As I mentioned in the answer, escape backslash (`\ `) or use raw string literal. – falsetru Nov 13 '16 at 04:54
  • @jingweimo use \\ or / or `os.path.join()` and you will no have problem with special chars . – furas Nov 13 '16 at 04:55
  • You should actually have only on backslash, so your comment is not correct. You only have to give two backslashes when defining a (non raw) string in code. – Klaus D. Nov 13 '16 at 04:55
  • You @peter I will post my complete codes tomorrow – jwm Nov 13 '16 at 04:55
  • R@ Kraus D. I do use \\ when I feed a root path in the function. But os.path.join change it somehow into that with \ – jwm Nov 13 '16 at 04:58
  • @falsetru I see the reason. Thanks – jwm Nov 13 '16 at 05:11