general coding issues - coding style...

JW John-Whitlock at ieee.org
Mon Feb 20 15:33:31 EST 2006


About this line:
1585 if sys.path[0][-12:] == "\library.zip":  #for py2exe

pl... suggested:
if sys.path[0].endswith( "\\library.zip" ):

and said, "did you really mean one back-slash there?".  You responded
"yeah, one backslash", but I still don't believe you.  In this case, it
happens to work, but you should be aware that the back-slash is an
escape character, which causes the next character to be interpreted
differently.  Try this in your interpreter:

print "\a" # System bell - might cause your speaker to beep
print "\t"  # Tab character
print "\n" # Newline character / sequence

See http://www.python.org/doc/2.4.2/ref/strings.html for more details
on the escape sequences that Python recognizes.  Here's a summary: if
the backslash + character is a special escape code, then replace it
with that, otherwise assume the programmer meant a real backslash.
That's dangerous, and will break when the name changes from one that
starts with an L to one that starts with an A, B, F, N, etc.  The safe
way it to tell Python "Yes, I really want a backslash", which is
indicated with the double backslash:

print "\\library.zip"

If you don't use the double backslash, you'll eventually have a
problem, especially in Windows, which unfortunately uses the backslash
as a directory seperator.  You might also want to look at os.sep and
the os.path.* functions, if you are interested in making your code work
on different platforms.  

JW




More information about the Python-list mailing list