PDA

View Full Version : Python file renaming script


misterwolfy
11-23-2011, 06:29 AM
This is not a thread related directly to Maya, but related to a script I use to modify files I might use with Maya.

I tend to rename a lot of files for various purposes while creating CG. I have used various tools to do this, most recently shell scripts, but now I am trying to improve my Python a bit, so I just wrote this script. Please use it, or abuse it. By abuse it, I mean tell me all the places where I could be more efficient, look like less of a python noob, or be more pythonic.

Seems to choke if there is a directory in the folder where the files reside. Need to figure that out.

Thanks for reading.


# Python renamer script: creates a function called pyrename(). pyrename() will rename all files in a given folder.
# Usage: Put all the files you want to rename in an isolated folder. The function can be called by typing pyrename().
# Warning, this script will rename your files. There is no undo. Use with care.
def pyrename():
'Put all the files you want to rename in an isolated folder. The function can be called by typing pyrename().'
import os

#function to ignore the hidden . files in a directory. Note the use of the 'yield' keyword
def listdir_nohidden(path):
for f in os.listdir(path):
if not f.startswith('.'):
yield f

path = raw_input('path to folder?: ')
#get the files from the folder and put the filenames in a list called files
theFiles = listdir_nohidden(path)
files = []
for f in theFiles:
files.append(f)

#user supplied values
print 'Want to replace a character or string in your file names?'
want_to_replace = raw_input('Type y or n. Or to completely rename type w: ')
if want_to_replace == 'y':
replace = raw_input('Type the character or string that you want to replace (FYI can be a space!): ')
replace_with = raw_input('Type the character or string that you want to replace with: ')
elif want_to_replace == 'w':
replace = ''
replace_with = raw_input('Type new name: ')
else:
replace = ''
replace_with = ''

if want_to_replace != 'w':
want_numbers = raw_input('Want your files numbered? type y or n: ')
if want_numbers == 'y':
zeros = raw_input('Type the amount of padding zeros you need (using a single integer, like 4): ')
else:
zeros = 0

if want_to_replace == 'w':
zeros = raw_input('Type the amount of padding zeros you need (using a single integer, like "4"): ')

ext = raw_input('Please type the three letter extension you want to use ex: jpg (NOT the .): ')


#remove extension, put the file names in a list called names
names = []
for f in files:
if f[-4] == '.':
names.append(f.replace(f[-4:], ''))

#add new names, add user supplied extension, put the file names in a list called namesPlusEx
namesPlusEx = []
count = 0
for f in names:
if want_to_replace == 'w':
namesPlusEx.append(f.replace(f, replace_with)+ (('.%.')+zeros+('d'))% count +'.'+ ext)
elif want_to_replace != 'w' and want_numbers == 'y':
namesPlusEx.append(f.replace(replace, replace_with)+ (('.%.')+zeros+('d'))% count +'.'+ ext)
else:
namesPlusEx.append(f.replace(replace, replace_with)+'.'+ ext)
count += 1

#rename the actual files
c=0
for f in files:
os.rename(path+'/'+f, path+'/'+namesPlusEx[c])
c+=1

print 'You have re-named %d files' % len(files)

CGTalk Moderation
11-23-2011, 06:29 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.