python脚本:批量重命名文件名
最近在跟着师姐做一个项目,需要不断地抠样本,用于学习训练。每次扣完样本后都需要重新批量命名一下,虽然Windows7可以批量重命名,但是文件一多的时候就很容易发生资源管理器卡死了。。。因此,决定写个python的脚本,节省下以后重命名的时间。
代码
#!\usr\bin\env python
# -*- coding: utf-8 -*-
# Author: PZX
# FileName: batchrename.py
# Function: 批量命名某一文件夹下的文件名
import sys
import os
def UsePrompt():
#如果省略path,则path为当前路径
print 'Useage: batchrename.py [path] newfilenames'
sys.exit()
def BatchRename(path, pattern):
#设置路径
os.chdir(path)
fileList = os.listdir(path)
dotIndex = pattern.rfind('.')
fileName = pattern[ : dotIndex]
fileExt = pattern[dotIndex : ]
genNum = 0
for fileItem in fileList:
fileFullName = fileName + ' (' + str(genNum) + ')' + fileExt
os.rename(fileItem, fileFullName)
print (fileItem + ' => ' + fileFullName)
genNum += 1
def main():
if len(sys.argv) == 3:
path = sys.argv[1]
pattern = sys.argv[2]
elif len(sys.argv) == 2:
path = os.getcwd()
pattern = sys.argv[1]
else:
UsePrompt()
confirm = raw_input('Confirm(y|n): ')
if confirm == 'n':
sys.exit()
BatchRename(path, pattern)
if __name__ == '__main__':
main()
新增:
添加撤销上次批量重命名功能。使用方法:batchrename.py /r
#!\usr\bin\env python
# -*- coding: utf-8 -*-
# Author: PZX
# FileName: batchrename.py
# Function: 批量命名某一文件夹下的文件名
import sys
import os
import os.path
import pickle
def UsePrompt():
#如果省略path,则path为当前路径
print 'Useage: batchrename.py [path] [newfilenames | /r]'
print '\tuse /r to cancel the renaming operation'
sys.exit()
def BatchRename(path, pattern):
#设置路径
os.chdir(path)
fileList = os.listdir(path)
dotIndex = pattern.rfind('.')
fileName = pattern[ : dotIndex]
fileExt = pattern[dotIndex : ]
genNum = 0
fileNameDict = {}
for fileItem in fileList:
fileFullName = fileName + ' (' + str(genNum) + ')' + fileExt
os.rename(fileItem, fileFullName)
genNum += 1
print (str(genNum) + ': ' + fileItem + ' => ' + fileFullName)
fileNameDict[fileItem] = fileFullName
#保存重命名的记录
recordDir = os.path.dirname(__file__)
recordPath = os.path.join(recordDir, 'batchrenamerecord')
recordFile = file(recordPath, 'wb')
pickle.dump(path, recordFile, pickle.HIGHEST_PROTOCOL)
pickle.dump(fileNameDict, recordFile, pickle.HIGHEST_PROTOCOL)
recordFile.close()
def CancelBatchRename():
#读取上一次重命名的记录文件
recordDir = os.path.dirname(__file__)
recordPath = os.path.join(recordDir, 'batchrenamerecord')
recordFile = file(recordPath, 'rb')
path = pickle.load(recordFile)
fileNameDict = pickle.load(recordFile)
recordFile.close()
os.chdir(path)
#撤销上一次重命名
genNum = 0
newFileNameDict = {}
for orgName, newName in fileNameDict.items():
os.rename(newName, orgName)
genNum += 1
print (str(genNum) + ': ' + newName + ' => ' + orgName)
newFileNameDict[newName] = orgName
recordDir = os.path.dirname(__file__)
recordPath = os.path.join(recordDir, 'batchrenamerecord')
recordFile = file(recordPath, 'wb')
pickle.dump(path, recordFile, pickle.HIGHEST_PROTOCOL)
pickle.dump(newFileNameDict, recordFile, pickle.HIGHEST_PROTOCOL)
recordFile.close()
def main():
cancelling = False
if len(sys.argv) == 3:
path = sys.argv[1]
pattern = sys.argv[2]
elif len(sys.argv) == 2:
if sys.argv[1] == '/r':
cancelling = True
else:
path = os.getcwd()
pattern = sys.argv[1]
else:
UsePrompt()
confirm = raw_input('Confirm(y|n): ')
if confirm == 'n':
sys.exit()
if cancelling:
CancelBatchRename()
else:
BatchRename(path, pattern)
if __name__ == '__main__':
main()
作者: pzxbc
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。