Two problems with backslashes

gbreed at cix.compulink.co.uk gbreed at cix.compulink.co.uk
Wed Jul 4 11:23:45 EDT 2001


In article <Xns90D4A141E71CDgustaflalgonetse at 194.213.69.148>, 
gustafl at algonet.se (Gustaf Liljegren) wrote:

> I have two problems with backslashes in DOS path strings:
> 
> >>> path = 'e:\test\test.txt'
> >>> path.replace('\\', '/')
> 'e:\test\test.txt'
> 
> Expected:
> 
> 'e:/test/test.txt'

I see you've doubled the backslash in '\\'.  So do the same in path

>>> path = 'e:\\test\\test.txt'

for the same reason.  Or

>>> path = r'e:\test\test.txt'

> Also, split() doesn't like backslashes:
> 
> >>> paths = 'e:\test\file1.txt e:\test\file2.txt'
> >>> paths.split()
> ['e:', 'est', 'ile1.txt', 'e:', 'est', 'ile2.txt']
> 
> Expected:
> 
> ['e:\test\file1.txt', 'e:\test\file2.txt']
> 
> Alternative ways would be much appreciated.

Same problem, different symptoms.  \t and\f are both treated as 
whitespace.  Try

>>> paths = r'e:\test\file1.txt e:\test\file2.txt'


                      Graham



More information about the Python-list mailing list