Re: removing spaces from front and end of filenames
by Stephen Horne other posts by this author
Jul 13 2003 4:01PM messages near this date
Re: removing spaces from front and end of filenames
|
Re: removing spaces from front and end of filenames
On 13 Jul 2003 08:44:05 -0700, hokiegal99@[...].com (hokiegal99)
wrote:
> Erik Max Francis <max@[...].com> wrote in message news:<3F10BABB.D548961B@[...].com>...
> > 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?
If you mean looking at the list, the stripped results aren't in there
because you didn't put them there. See my other reply.
If you literally mean 'in the directory' (ie looking using a file
browser) you need to do yet another step - to apply the stripped names
back to the files using the 'os.rename' function. I'm not familiar
with os.walk and can't find the documentation, so the following is
probably wrong, but I'd suggest something like...
for root, dirs, files in os.walk('/home/rbt/scripts'):
for file in files:
os.rename(file, file.strip ())
This does seem very unlikely, though.
--
http://mail.python.org/mailman/listinfo/python-list
Thread:
hokiegal99
Bengt Richter
Bengt Richter
Bengt Richter
hokiegal99
Stephen Horne
Stephen Horne
Erik Max Francis
hokiegal99
hokiegal99
Jeff Epler
Erik Max Francis
|