FAQ
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\\')

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-list/attachments/20070723/11eb139c/attachment.htm

Search Discussions

  • Miles at Jul 23, 2007 at 7:21 pm

    On 7/23/07, Randy Kreuziger 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
  • Carsten Haese at Jul 23, 2007 at 7:24 pm

    On Mon, 2007-07-23 at 11:41 -0700, Randy Kreuziger 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\\')
    You're using the wrong tool for the job. lstrip removes any contiguous
    string consisting of any of the characters in the string you give it.
    You would have achieved the same wrong result with

    teststring.lstrip(" 08:C\\adehilnoprsvx")

    As you have discovered, this hungry caterpillar will eat the leading
    zeroes of your filename. It doesn't see any difference between the
    zeroes before the last \ and the zeroes after the last \. It just eats
    them all. If the leading zeroes had been followed by an 8, it would have
    eaten that, too.

    The right tool for the job is os.path, more specifically
    os.path.basename(teststring).

    HTH,
  • Steven D'Aprano at Jul 23, 2007 at 11:28 pm

    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.
  • Randy Kreuziger at Jul 23, 2007 at 11:55 pm
    Thanks for the basename suggestion. That almost works. I'm running into a problem with some of the directory names when they include \800x\ see below.
    ?-----------------------------------------------------------------------------
    import sys, os, string

    teststring = 'C:\shoreline\dvd\prep area\800x\060623_00001.jpg'
    print os.path.basename(teststring)
    ?-----------------------------------------------------------------------------
    Results in
    800x0623_00001.jpg

    But it needs to be
    060623_00001.jpg





    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://mail.python.org/pipermail/python-list/attachments/20070723/020b1da7/attachment.htm
  • Steve Holden at Jul 24, 2007 at 12:37 am

    Randy Kreuziger wrote:
    Thanks for the basename suggestion. That almost works. I'm running
    into a problem with some of the directory names when they include
    \800x\ see below.
    ?-----------------------------------------------------------------------------
    import sys, os, string

    teststring = 'C:\shoreline\dvd\prep area\800x\060623_00001.jpg'
    print os.path.basename(teststring)
    ?-----------------------------------------------------------------------------
    Results in
    800x0623_00001.jpg

    But it needs to be
    060623_00001.jpg
    You need to be careful with those backslashes!
    teststring = 'C:\shoreline\dvd\prep area\800x\060623_00001.jpg'
    teststring
    'C:\\shoreline\\dvd\\prep area\\800x0623_00001.jpg'
    teststring = r'C:\shoreline\dvd\prep area\800x\060623_00001.jpg'
    teststring
    'C:\\shoreline\\dvd\\prep area\\800x\\060623_00001.jpg'
    >>>

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    --------------- Asciimercial ------------------
    Get on the web: Blog, lens and tag the Internet
    Many services currently offer free registration
    ----------- Thank You for Reading -------------

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJul 23, '07 at 6:41p
activeJul 24, '07 at 12:37a
posts6
users5
websitepython.org

People

Translate

site design / logo © 2021 Grokbase