some path issues on windows
John Machin
sjmachin at lexicon.net
Fri Mar 28 23:30:45 EDT 2008
On Mar 29, 1:31 pm, Brad <b... at 16systems.com> wrote:
> When reading a file into a list that contains windows file paths like this:
>
> c:\documents and settings\brad\desktop\added_software\asus\a.txt
Do you mean "reading a file into a list that AS A RESULT
contains ..."? If not, what do you mean?
>
> I get a list that contains paths that look like this:
>
> c:\\documents and settings\\brad\\desktop\\added_software\\asus\\a.txt
>
> So, my list contains those funky file paths.
Only if your file already contains contains the doubled backslashes.
> And, when I do this:
>
> for root, dirs, files in os.walk('C:\\'):
> for f in files:
> print os.path.join(root,f)
>
> I get the more normal path again:
>
> c:\documents and settings\brad\desktop\added_software\asus\a.txt
>
> This makes path comparison difficult as I have different strings (the
> extra \'s). I've tried os.path.normpath(line_reading_in_from_file) but I
> still get the funky \\ paths. How can I read the paths with an r'path'
> like meaning?
File reading operations *don't* magically double backslashes. Believe
me. Show us the code that is reading the file. Show us the contents of
the file. Assure us that you understand what is going on here:
>>> alist = ['a\\b', r'a\b']
>>> alist[0] == alist[1]
True
>>> print alist
['a\\b', 'a\\b']
# So if you see doubled backslashes when you do print alist,
# there are really only single backslashes.
>>> print alist[0], alist[1]
a\b a\b
>>> print repr(alist[0]), repr(alist[1])
'a\\b' 'a\\b'
HTH,
John
More information about the Python-list
mailing list