Thread: OT: Python
View Single Post
  #3  
Old October 16th, 2006, 03:24 PM
Fyron's Avatar

Fyron Fyron is offline
Shrapnel Fanatic
 
Join Date: Jul 2001
Location: Southern CA, USA
Posts: 18,394
Thanks: 0
Thanked 12 Times in 10 Posts
Fyron is an unknown quantity at this point
Default Re: OT: Python

Personally, I'd use CK Rename for batch file renaming. But in a python script, you would need something like:

Code:
import os
import re

oldstring = 'whatever'
newstring = 'new'

name_match = re.compile(oldstring)

os.chdir('target')

print 'preop:'
print os.listdir('.')

for f in os.listdir('.'):
os.rename(f, name_match.sub(newstring, f, count=1))

print 'postop:'
print os.listdir('.')



Make sure to replace 'target' with the directory containing the files.

Alternatively without much extraneous bits:

Code:
import os
import re

name_match = re.compile('whatever')

os.chdir('target')

for f in os.listdir('.'):
os.rename(f, name_match.sub('new', f, count=1))



reference:
http://docs.python.org/lib/os-file-dir.html
__________________
It's not whether you win or lose that counts: it's how much pain you inflict along the way.
--- SpaceEmpires.net --- RSS --- SEnet ModWorks --- SEIV Modding 101 Tutorial
--- Join us in the #SpaceEmpires IRC channel on the Freenode IRC network.
--- Due to restrictively low sig limits, you must visit this link to view the rest of my signature.
Reply With Quote