您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

重命名批处理python脚本

2024/3/31 4:46:36发布11次查看
将copy of ********.bmp或者copy of copy of ********.bmp 此类文件统一命名为********0.bmp 或者********00.bmp等格式,后面的0的个数代表********.bmp出现的次数+1。写了个下面的小脚本:
代码如下:
import os
这个程序是用来将文件名改名,因为在文件夹里面有很多copy of 重文件名,因此去掉windows
重命名风格而换用新的累加命名机制
作者:chenxofhit@gmail.com
时间:2011年4月13日
def getfilenames(dirs, dict):
#dir为目录名,lst列表
filenames=os.listdir(dirs)
for name in filenames:
key = filenames[filenames.index(name)]
sign = copy of
judge = key.find(sign)
if (judge != -1 ):
dict[key] = 1
else: #提取其中的真实文件名
truekey= key[-12:] #因为真实的文件名都是在12位
if truekey not in dict: #字典中不存在
dict[truekey]=1
os.rename(dirs+str(os.sep)+name,dirs+str(os.sep)+truekey)
else:
#split finames
newkey= str(truekey[:-4])+str('0'*dict[truekey])+ str(truekey[-3:])
dict[truekey] = dict[truekey]+1
os.rename(dirs+os.sep+name,dirs+os.sep+newkey)
if '__name__=__main__':
dict={}
dirs = c://temp
getfilenames(dirs, dict)
用到了字典,用到了os模块,学到了一些东西,呵呵!
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product