string - Python: os.join.path() -
this question has answer here:
- why backslashes appear twice? 1 answer
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
Post a Comment