Maintaining leading zeros with the lstrip string function?

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Mon Jul 23 19:28:51 EDT 2007


On Mon, 23 Jul 2007 15:21:20 -0400, Miles wrote:

> Also, use raw strings ( r'\path\to\file' ) to avoid problems
> with backslashes being interpreted in strings.

Not quite. Raw strings are designed for building regular expressions, not
file names. Consequently, there are still a few cases where they won't
help you, e.g.:

>>> fname = 'mydoc.txt'
>>> fname = r'C:\My Documents\Something\' + fname
  File "<stdin>", line 1
    fname = r'C:\My Documents\Something\' + fname
                                                ^
SyntaxError: EOL while scanning single-quoted string

A better solution is to remember that Windows will accept a forward slash
anywhere it expects a backslash:


>>> fname = 'C:/My Documents/Something/' + fname     

(But again... the better, platform independent way to do this is with
os.path.)

-- 
Steven.




More information about the Python-list mailing list