ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-list
python-list
Re: removing spaces from front and end of filenames
by hokiegal99 other posts by this author
Jul 14 2003 12:33AM messages near this date
Re: removing spaces from front and end of filenames | Re: removing spaces from front and end of filenames
Bengt Richter wrote:
>  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?
>  
>  I don't see where you rename " test " to "test" ;-)
>  
>  BTW, file is a builtin name for the file class, which creates open file objects,
>  so it's best to use another name.
>  
>  Maybe change that last loop to (untested!)
>  
>  for root, dirs, files in os.walk('/home/rbt/scripts'):
>      for fname in files:
>          newfile = fname.strip( )
>          if newfile != fname:
>              newpath = os.path.join(root,newfile)
>              oldpath = os.path.join(root,fname)
>              os.rename(oldpath,newpath)
>              print `oldpath` # back ticks to print repr to make sure you can see spaces
>  	    print `newpath`
>  
>  Regards,
>  Bengt Richter

I've found the below code to OK, does anyone see any problems with it? 
I've ran it several times w/o damaging anything ;). The only problem 
with doing this on dirs is that the script doesn't act on dirs within 
dirs that are being renamed by the script as the path didn't exist when 
the script started running. So, I have to run it several times. It works 
but it's a bit of a kludge. Anyone know of a work around for this?


for root, dirs, files in os.walk('/home/rbt/test'):
     for dir in dirs:
         old_dname = (dir)
         new_dname = old_dname.strip( )
	newdir = new_dname
	if newdir <>  old_dname:
             newpath = os.path.join(root,newdir)
             oldpath = os.path.join(root,dir)
             os.rename(oldpath,newpath)
	    print oldpath
	    print newpath

-- 
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

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved