I run an external function that returns a Windows path to a file on disk as a string (part of the string:
Error details are at "C:\Users\ADMINI~1\AppData\Local\Temp\2\BuildErrors.txt" Succeeded
So, I load the result returned into a string variable:
s = '''Error details are at "C:\Users\ADMINI~1\AppData\Local\Temp\2\BuildErrors.txt" Succeeded'''
file_path = s.split('"')[1]
print file_path
> C:\Users\ADMINI~1\AppData\Local\Temp\BuildErrors.txt
#(with STX icon after Temp
If I access the file_path
in the Python Shell, its printed like this:
file_path
'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\x02\\BuildErrors.txt'
I understand that \2
is handled as a special character in Python, but this makes it impossible to for me to read the file as the path is not valid.
As I am getting the string from external function, I already have a string object and as far as I know, you cannot make a raw string (r''
) from that.
I've tried s.encode('string-escape')
on the source string, but it keeps the \x02
in place.
How do produce a valid path by handling the \2
in it?