some path issues on windows

B

Brad

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

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. 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?

Thanks,
Brad
 
S

Steven D'Aprano

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

I get a list that contains paths that look like this:

c:\\documents and settings\\brad\\desktop\\added_software\\asus\\a.txt


What makes you think that there are doubled backslashes in the string?
Look at this:

16

Despite how it looks, the backslashes are all singles, not doubles:
1

When you display strings, by default special characters are escaped,
including backslashes. (Otherwise, how could you tell whether the \f in
the path meant backslash followed by f or a formfeed?)

Python interprets backslashes as special when reading string literals,
NOT when reading them from a file:
10
 
J

John Machin

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
 
B

Brad

Steven said:
What makes you think that there are doubled backslashes in the string?
Look at this:

Sorry, my mistake... when testing with prints, printing the list itself
showed double backslashes \\ in the paths, but when printing each path
individually, it's only one back... should have tested more before
posting. My apologies.

Brad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top