"import" not working?

Rhodri James rhodri at wildebst.demon.co.uk
Mon Feb 23 19:18:50 EST 2009


On Mon, 23 Feb 2009 19:24:46 -0000, Lionel <lionel.keene at gmail.com> wrote:

> sys.path.append("C:\DataFileTypes")

Just so that we're clear, this is a *really* *bad* habit to get
into.  Not appending to sys.path, though that isn't often a good
idea, but failing to escape your backslashes.  This works because
'\D' happens not to be a valid escape sequence: if your directory
had instead been called "newtypes" then "C:\newtypes" would not
have had the result you were expecting at all.  If you really
mean a backslash to be in any literal string, you should always
double it:

sys.path.append("C:\\DataFileTypes")

IMHO, Python is somewhat inconsistent in not producing a
compile-type error (or at least an annoying compile-time
warning) when presented with invalid escape sequences.
What it does, even though it's well-documented and usually
the right guess, is to encourage bad habits.

-- 
Rhodri James *-* Wildebeeste Herder to the Masses



More information about the Python-list mailing list