Maintaining leading zeros with the lstrip string function?

Miles semanticist at gmail.com
Mon Jul 23 15:21:20 EDT 2007


On 7/23/07, Randy Kreuziger <KREUZRSK at dfw.wa.gov> wrote:
> I need just the file name from a string containing the path to a file.  The
> name of the file starts with zeros.  This is problematic because the  lstrip
> function strips them leaving this as the result:
> 6128.jpg
>
>
> How do I strip the path without losing the leading zeros in the file name?
>
>  ---------------------------------------------
> import sys, os, win32com.client, string
>
> teststring = 'C:\shoreline\dvd\prep area\800x\\006128.jpg'
> print string.lstrip(teststring, 'C:\shoreline\dvd\prep area\800x\\')

lstrip removes *any* of the set of characters in the argument, not the
exact string[1].  Use of the string module for lstrip and other
functions[2] has been deprecated for using the string methods
directly, i.e. teststring.lstrip(...).

You should probably be using split (or basename) in the os.path
module.  Also, use raw strings ( r'\path\to\file' ) to avoid problems
with backslashes being interpreted in strings.

-Miles

[1] http://docs.python.org/lib/string-methods.html
[2] http://docs.python.org/lib/node42.html



More information about the Python-list mailing list