Re: removing spaces from front and end of filenames

Discussion in 'Python' started by tubby tudor, Jul 13, 2003.

  1. tubby tudor

    tubby tudor Guest

    Thanks for the tip Jeff. That's the same conclusion that I cam to. I'm new
    to python and programming in general, but having a language as intuitive as
    python and a list as helpful as this makes it easy. I can actually solve
    problems. Thanks again.


    >From: Jeff Epler <>
    >To: hokiegal99 <>
    >CC:
    >Subject: Re: removing spaces from front and end of filenames
    >Date: Sun, 13 Jul 2003 11:18:50 -0500
    >
    >On Sun, Jul 13, 2003 at 08:44:05AM -0700, hokiegal99 wrote:
    > > Erik Max Francis <> wrote in message

    >news:<>...
    > > > hokiegal99 wrote:
    > > >
    > > > > This script works as I expect, except for the last section. I want

    >the
    > > > > last section to actually remove all spaces from the front and/or end
    > > > > of
    > > > > filenames. For example, a file that was named " test " would be
    > > > > renamed "test" (the 2 spaces before and after the filename removed).
    > > > > Any
    > > > > suggestions on how to do this?
    > > >
    > > > That's what the .strip method, which is what you're using, does. If
    > > > it's not working for you you're doing something else wrong.

    > >
    > > for root, dirs, files in os.walk('/home/rbt/scripts'):
    > > for file in files:
    > > fname = (file)
    > > fname = fname.strip( )
    > > print fname
    > >
    > > When I print fname, it prints the filenames w/o spaces (a file named "
    > > test " looks like "test"), but when I ls the actual files in the
    > > directory they still contain spaces at both ends. That's what I don't
    > > understand. It seems that .strip is ready to remove the spaces, but
    > > that it needs one more step to actually do so. Any ideas?

    >
    >Surely you need to actually rename the file:
    > for root, dirs, files in os.walk('/home/rbt/scripts'):
    > for name in files:
    > newname = name.strip()
    > if newname != name: os.rename(name, newname)
    >
    >Jeff


    _________________________________________________________________
    The new MSN 8: advanced junk mail protection and 2 months FREE*
    http://join.msn.com/?page=features/junkmail
    tubby tudor, Jul 13, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. B.J.
    Replies:
    4
    Views:
    725
    Toby Inkster
    Apr 23, 2005
  2. hokiegal99
    Replies:
    9
    Views:
    547
    Bengt Richter
    Jul 14, 2003
  3. Karen Sundquist
    Replies:
    1
    Views:
    140
    Saurabh Nandu
    Dec 1, 2003
  4. Matt

    IIS ASP is front end or back end?

    Matt, Apr 4, 2004, in forum: ASP General
    Replies:
    3
    Views:
    237
    Ken VdB
    Apr 5, 2004
  5. The alMIGHTY N

    back-end vs. front-end calculation

    The alMIGHTY N, Apr 2, 2007, in forum: Javascript
    Replies:
    5
    Views:
    411
    The alMIGHTY N
    Apr 3, 2007
Loading...

Share This Page