.com.unity Forums

.com.unity Forums (http://forum.shrapnelgames.com/index.php)
-   Space Empires: IV & V (http://forum.shrapnelgames.com/forumdisplay.php?f=20)
-   -   OT: Python (http://forum.shrapnelgames.com/showthread.php?t=31034)

AngleWyrm October 16th, 2006 03:02 PM

OT: Python
 
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

Re: OT: Python
 
CKrename is a handy little program that does this and more... google it!

Fyron October 16th, 2006 03:24 PM

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

AngleWyrm October 16th, 2006 03:44 PM

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.