Windows file paths, again

Ethan Furman ethan at stoneleaf.us
Wed Oct 21 14:26:38 EDT 2009


Dan Guido wrote:
> I'm trying to write a few methods that normalize Windows file paths.
> I've gotten it to work in 99% of the cases, but it seems like my code
> still chokes on '\x'. I've pasted my code below, can someone help me
> figure out a better way to write this? This seems overly complicated
> for such a simple problem...
> 

[snip]

> 	test12 = "C:\windows\system32\xeuwhatever.sys" #this breaks everything

[snip]

> --
> Dan Guido

That is overly complicated.  I would recommend you use either raw 
strings for windows paths, or double backslashes.

The problem you are observing is that \x (unlike the simpler ones such 
as \t) takes a hex number after the \x, so the whole thing would be, for 
example, \xa9.  Because Python is looking for two hex digits after the 
back-slash, and not finding them, you get the error (long before your 
'fix-it' routine gets a chance to run).

Hope this helps.

~Ethan~



More information about the Python-list mailing list