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')
相关文章
- Python做磁盘文件服务器 2012/11/20
- IIS7.0+Python环境搭建步骤 2012/11/19
- 在IIS中如何执行Python脚本 2012/11/19
- 谈Python集合运算 2012/11/17
- Python中lambda表达式使用方法 2012/11/17
- Python调用短信猫控件发短信 2012/11/17
- Python模块之StringIO 2012/11/16
- Python根据一个日期获得星期几 2012/11/16
- Python Django发送邮件的配置方法 2012/11/16
- Python获得命令行参数的方法 2012/11/16