Re: removing spaces from front and end of filenames
by Bengt Richter other posts by this author
Jul 14 2003 3:51PM messages near this date
removing spaces from front and end of filenames
|
Re: removing spaces from front and end of filenames
On 14 Jul 2003 00:38:56 GMT, bokr@[...].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
--
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
|