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

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -