import glob, os ''' execfile("C:\\MyPy\\Scripts\\RenameFilesInFolder.py") ''' ''' directoryAddress=r'C:\Users\jborthen\Desktop\XV12Media\Animation_Principal_Characteristics\Capacity_Annotation_Animation' fileNameLength=4 n=449 # Rename all files in the directoryAddress folder to incremental integers starting at "n": def rename(dir, pattern, nameLen, n): i=n for pathAndFilename in glob.iglob(os.path.join(dir, pattern)): title, ext = os.path.splitext(os.path.basename(pathAndFilename)) os.rename(pathAndFilename, os.path.join(dir, ((nameLen-len(str(i)))*str(0))+str(i) + ext)) i=i+1 rename(directoryAddress, r'*.png', fileNameLength, n) ''' # === Replace all underscores with hyphens in any non-hidden file in the current directory: ''' import os [os.rename(f, f.replace('_', '-')) for f in os.listdir('.') if not f.startswith('.')] ''' # === List files recursively. If the last character is an alphebetic character change it to lowercase and rename the file: ''' def listFiles(dir): rootdir = dir for root, subFolders, files in os.walk(rootdir): for file in files: yield os.path.join(root,file) return for f in listFiles("C:\\MyPy\\Scripts"): fname=f.split("\\")[-1] if fname[0].isalpha(): os.rename(f,f+fname[0].upper()+fname[1:]) print "Renamed " + f + " to " + f+fname[0].upper()+fname[1:] ''' # === List files recursively. Remove first half of a file's name. def listFiles(dir): rootdir = dir for root, subFolders, files in os.walk(rootdir): for file in files: yield os.path.join(root,file) return for f in listFiles("C:\\MyPy\\Scripts\\PyInstaller-2.1"): fname=f.split("\\")[-1] if raw_input("Rename "+fname+"? ").lower() == "y": os.rename(f,f[:-len(fname)]+f.split("\\")[-1][len(fname)/2:]) print "Renamed " + f + " to " + f[:-len(fname)]+fname[(len(fname)/2):]