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 Bengt Richter other posts by this author
Jul 14 2003 12:38AM 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 09:43:46 -0700, hokiegal99@[...].com (hokiegal99) wrote:

> Ha!!
> 
> Fixed it with this bit of code:
> 
> for root, dirs, files in os.walk('/home/BradTill/python'):
>     for file in files:
>         fname = (file)
>         fname = fname.strip( )
> 	newfile = fname
> 	if newfile:
     for fname in files:
         newfile = fname.strip()
         if newfile!=fname:  
> 	    newpath = os.path.join(root,newfile)
>             oldpath = os.path.join(root,file)
>             os.rename(oldpath,newpath)
> 	    print oldpath
> 	    print newpath
> 
I'd suggest using four spaces instead of tabs ;-)

Why not do the whole thing in one loop? (Ignore my prev post suggestion for final
renaming loop just for spaces):

#XXX# untested !!
import re, os
percent2f_n_bad = re.compile(r'%2f|[*?<> /|\\]') # look for bad chars too
for root, dirs, files in os.walk('/home/rbt/scripts'):
     for fname in files:  
          newfile = percent2f_n_bad.sub('-', fname)
          newfile.strip() # and the space thing
          if newfile != fname: # you really only need to know if something changed, right?
              newpath = os.path.join(root,newfile)
              oldpath = os.path.join(root,fname)
              os.rename(oldpath,newpath)
              print `oldpath` # backticks to get quoted repr, to see spaces
              print `newpath`

Or am I missing something?

Regards,
Bengt Richter
-- 
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