![]() |
OT: Python
With Python,
How would I change a directory of filenames from whatever_stuff.txt to new_stuff.txt? |
Re: OT: Python
CKrename is a handy little program that does this and more... google it!
|
Re: OT: Python
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 |
Re: OT: Python
Thanks, big help!
|
All times are GMT -4. The time now is 02:56 AM. |
Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©1999 - 2025, Shrapnel Games, Inc. - All Rights Reserved.