Using repr() with escape sequences

nummertolv nummertolv at gmail.com
Thu Feb 23 10:32:36 EST 2006


Hi,

My application is receiving strings, representing windows paths, from
an external source. When using these paths, by for instance printing
them using str() (print path), the backslashes are naturally
interpreted as escape characters.

>>> print "d:\thedir"
d:	hedir

The solution is to use repr() instead of str():

>>> print repr("d:\thedir")
'd:\thedir'

What I have not been able to figure out is how to handle escape
sequences like \a, \b, \f, \v and \{any number} inside the paths. Using
repr() on these escape sequences either prints the hex value of the
character (if "unprintable" i guess) or some character ( like in the
last example below).

>>> print repr("d:\thedir\10")
'd:\thedir\x08'

>>> print repr("d:\thedir\foo")
'd:\thedir\x0coo'

>>> print repr("d:\thedir\100")
'd:\thedir@'

Could someone clear this out for me and let me know how I can find the
"real" path that I am trying to receive?

/Henrik




More information about the Python-list mailing list