ValueError: invalid \x escape

J. Cliff Dyer jcd at sdf.lonestar.org
Fri Nov 2 02:34:10 CET 2007


crybaby wrote:
> I wrote a python code in linux text pad and copied to thumb drive and
> try to ran the file by changing the path to windows:
>
> sys.path = sys.path + ['D:\Python24\Lib\site-packages\mycode]
>
> I get the following error:
>
> ValueError: invalid \x escape
>
> I am pretty sure this problem is due some kind of linux end of line
> markers or escape characters that windows os doesn't understand.  Is
> there a way to fix this?
>
>   
You have two errors in your code.  The first is that you didn't close
your quotes.  However, that wouldn't cause a value error, so you
probably introduced that one in your email.  The other is that you
haven't escaped your backslashes.  Python, unlike many other scripting
languages, does not distinguish between single and double quotes (except
insofar as there may be single or double quotes within the string, of
course), so python sees a \P, a \L a \s, and a \m in your string.  There
are two ways to solve this:

1)  Use a raw string: r'D:\Python24\Lib'
2) Escape your backslashes: 'D:\\Python24\\Lib'

I assume the real value of 'mycode' has an x immediately after a \, right?

Cheers,
Cliff


P.S.  I just got my crystal ball back from the cleaners, so I can guess
some of what you haven't told us, but in the future, make sure the code
you post exhibits the problem you see.  Use copy and paste instead of
retyping, and if you change the code, make sure the changed code still
exhibits the same problems.



More information about the Python-list mailing list