Python对文件批量随机重命名


Python #重命名2012-11-20 09:53

对所有文件进行重命名,随机命名,实现代码如下:

#RenameRand.py
#Yanggd http://yige.org
#get file name, and then rename the file
#after renamed, the name likes this:[a-zA-Z][a-zA-Z][0-9][0-9][a-zA-Z][0-9]

import random
import os

existName = []

def renameRand(path):
    fileList = os.listdir(path) #list the file name
    rangeLetter=('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
    for oldFileNameAndExt in fileList:
        newFileName = random.choice(rangeLetter)+random.choice(rangeLetter)+ \
        str(random.randrange(10))+str(random.randrange(10))+random.choice(rangeLetter)+str(random.randrange(10))
        while newFileName.upper() in existName:
            newFileName = random.choice(rangeLetter) + random.choice(rangeLetter)+ \
            str(random.randrange(10))+str(random.randrange(10))+random.choice(rangeLetter)+str(random.randrange(10))
        existName.append(newFileName.upper())
        [oldFileName, fileExt] = oldFileNameAndExt.split('.', 1)
        newFileNameAndExt = [newFileName, fileExt]
        newFileNameAndExt = '.'.join(newFileNameAndExt)
        path = os.path.abspath(path)
        pathSrc = path + '\\' + oldFileNameAndExt
        pathDst = path + '\\' + newFileNameAndExt
        os.rename(pathSrc, pathDst)

if __name__ == '__main__':
    renameRand(r'F:\Practice code\testData\rename')


相关文章

粤ICP备11097351号-1