Tuesday, June 06, 2006

Transcending into the next level

Diddlbiker got a little bit closer to Python-Zen today. Discovering that you have a folder with over two hundred files named '05????.txt' and '06????.txt' isn't really fun if they should be named 'm05????.txt' and 'm06?????.txt'. I surprised myself with conseriding this the easiest solution:

import os
myfolder = 'C:\\blablabla'
for myfile in os. listdir(myfolder):
__oldname = os.path.join(myfolder, myfile)
__newname = os.path.join(myfolder, 'm' + myfile)
__os.rename(oldname, newname)

(underscores used to indicate spaces; blogspot removes them no matter what I do, even nbsp's)
Time to write script: about 40 seconds - and done!

One day, I'll be a real Pythonista.

No comments: