AngleWyrm
October 16th, 2006, 03:02 PM
With Python,
How would I change a directory of filenames from whatever_stuff.txt to new_stuff.txt?
Captain Kwok
October 16th, 2006, 03:22 PM
CKrename is a handy little program that does this and more... google it!
Fyron
October 16th, 2006, 03:24 PM
Personally, I'd use CK Rename for batch file renaming. But in a python script, you would need something like:
<font class="small">Code:</font><hr /><pre>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('.')</pre><hr />
Make sure to replace 'target' with the directory containing the files.
Alternatively without much extraneous bits:
<font class="small">Code:</font><hr /><pre>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))</pre><hr />
reference:
http://docs.python.org/lib/os-file-dir.html
AngleWyrm
October 16th, 2006, 03:44 PM
Thanks, big help!
vBulletin® v3.8.1, Copyright ©2000-2025, Jelsoft Enterprises Ltd.