string - Python: os.join.path() -


this question has answer here:

i used os.join.path() load image in folder. found function cannot give accurate path when used in defining function in cases. 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 implement function, printed result of path is:

 mypath\apple.jpeg 

it illegal image loading. os.path.join() works in pythonconsole.

how make path generated in such function definition have double backslashes?

also, noted os.path.normpath cannot work sometimes. example:

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

it should give result:

 mypath\\apple 

but instead, results in:

 'mypath\x07pple' 

how come??

\a equivalent \x07. (see escape sequence part in string , bytes literals.)

>>> '\a' '\x07' 

you need escape \ mean backslash literally:

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

or, use raw string literal:

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

Comments

Popular posts from this blog

jsf - "PropertyNotWritableException: Illegal Syntax for Set Operation" error when setting value in bean -

arrays - Algorithm to find ideal starting spot in a circle -

php - Autoloader issue not returning Class -