ValueError: invalid \x escape

C

crybaby

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

J. Cliff Dyer

crybaby said:
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.
 

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,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top