removing spaces from front and end of filenames

Bengt Richter bokr at oz.net
Mon Jul 14 11:51:54 EDT 2003


On 14 Jul 2003 00:38:56 GMT, bokr at oz.net (Bengt Richter) wrote:
[...]
>
>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
#          ^^^^^^^^^^^^^^^ - does nothing useful like that ;-/
           newfile = newfile.strip() # argh, just noticed. D'oh. What else escaped? Sorry ;-/
>          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?
>
Well, I did miss a least one thing ;-/

Regards,
Bengt Richter




More information about the Python-list mailing list