python - How to make os.path.abspath(file) correct for folders beginning with "t" -


this question has answer here:

i use os.path.abspath(file) on file address entered via command line sanitize before handle file.

that means; performing following:

outputpath = 'c:\users\jehoshaphakshay\desktop' os.path.abspath(outputpath) 

gives following output:

'c:\\users\\jehoshaphakshay\\desktop' 

this in hope code more robust on different platforms , different kinds of user inputs.

recently ran issue approach doesn't work expected path has 1 of folders beginning letter t

that means; performing following:

outputpath = 'c:\users\jehoshaphakshay\desktop\temp' os.path.abspath(outputpath) 

gives following output:

'c:\\users\\jehoshaphakshay\\desktop\temp' 

how give me correct path -

'c:\\users\\jehoshaphakshay\\desktop\\temp' 

without doing find , replace not elegant?

additionally, don't mind using raw string literal long can prefix existing string raw string literal.

when os.path.abspath(file) parses string, first looks @ \{char} see if it's special escaped character, , if isn't one, treat normal part of path.

you need treat path raw string adding r"...." desired output:

path = r'c:\users\jehoshaphakshay\desktop\temp' os.path.abspath(path) >> 'c:\\users\\jehoshaphakshay\\desktop\\temp' 

basically, saying making raw string tell interpeter ignore special escaped characters , @ each char - character.

you can see succinctly in @phihag's answer here


Comments