-1

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?

Alex Tereshenkov
  • 3,340
  • 8
  • 36
  • 61
  • By explaining how you're getting the invalid path and letting others tell you what you're doing wrong. – Ignacio Vazquez-Abrams May 21 '16 at 06:10
  • Hm, I thought I did :) added more steps in creating variables if this is what you have thought of. – Alex Tereshenkov May 21 '16 at 06:18
  • Your edit is a bit confusing. **Exactly** how do you "load the result returned into a string variable"? If you're literally pasting it into a triple-quoted string in your Python source then you need to make that a raw triple-quoted string. And if you read the string from a file (or file-like entity) then the backslashes shouldn't be an issue. Perhaps it would help if you showed us `repr(s)`. – PM 2Ring May 21 '16 at 06:44
  • @PM2Ring, thanks for pointing this out. I've missed that I created a variable already damaged and my tests on a substring went wrong. Accessing the original string directly works fine, so my question is no longer relevant. Feel free to delete it as I can't do that for a question with answers. – Alex Tereshenkov May 21 '16 at 07:34
  • @AlexTereshenkov use raw triple quote (`r""" """`) strings for pasting. You're copy-pasting it after all. – Antti Haapala -- Слава Україні May 21 '16 at 07:57

2 Answers2

1

So you have a few things going on.

1) You should be using Python 3. Its time.

2) Monik's answer is correct if you just want to switch to unix style path separator characters. Python will let you use unix style paths on a windows system. Just remember that other windows shells and program will not.

3) Here is what is going on. If your string is in a file called fred.txt then

>>> with open('fred.txt') as f:
...  derf = f.readline()
...
>>> derf
'Error details are at "C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\2\\BuildErrors.txt" Succeeded'
>>> file_path = derf.split('"')[1]
>>> file_path
'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\2\\BuildErrors.txt'
>>> os.path.split(file_path)
('C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\2', 'BuildErrors.txt')
>>>

Then everything seems to work ok. The python shell displays the double slashes because of how it stores strings internally. If you write that value to a file or print it to the screen you get

>>> print(file_path)
C:\Users\ADMINI~1\AppData\Local\Temp\2\BuildErrors.txt
>>>

So now we get to the crux of the problem. The slash character '\' has special meaning in python strings. It is used to tell the system, hey what follows might be different. So I can specify characters that don't appear on my keyboard via hexadecimal or unicode. for example 3 ways to define a pound sign in a string. I recommend reading http://python-reference.readthedocs.io/en/latest/docs/str/escapes.html

>>> a = "#"
>>> b = "\x23"
>>> c = "\u0023"
>>> a
'#'
>>> b
'#'
>>> c
'#'
>>> a == b
True
>>> a == c
True
>>> b == c
True
>>> 

So if '\' has a special meaning, how do I tell the system that I really just want a slash? You escape it! '\\' in the python shell just says I want a slash.

>>> s = "\\"
>>> s
'\\'
>>> print(s)
\
>>>
WombatPM
  • 2,561
  • 2
  • 22
  • 22
0

If your string path is 'C:\ABC\xyz.txt' then the statement, it gives you 'C:\\ABC\\xyz.txt'

To make it a valid path for python file handling, it should be in format of C:/ABC/xyz.txt

So if path = 'C:\\ABC\\xyz.txt'

path = path.replace("\\","/")

and path is in right format.

bmonikraj
  • 52
  • 4