Python 文件批量改名

#'Peo sp20070701.mp3'改为'sp20070701_Peo.mp3'

import os
import sys

path = 'E:\\English\\'
os.chdir(path)
fileList = os.listdir(path)
print fileList

confirm = raw_input('Confirm(y|n): ')
if confirm == 'n':
sys.exit()

for fileItem in fileList:
dotIndex = fileItem.rfind('.')
blkIndex = fileItem.rfind(' ')
fileName1 = fileItem[ : blkIndex]
fileName2 = fileItem[blkIndex + 1 : dotIndex]
fileExt = fileItem[dotIndex : ]
if len(fileName1)<4:
newname = fileName2 + '_' + fileName1 + fileExt
os.rename(os.path.join(path,fileItem),os.path.join(path,newname))
print (fileItem + ' => ' + newname)

 

#Error 32是文件已经打开的错误,Error 183是文件已存在的问题

posted on 2012-06-30 06:10  ibook365  阅读(1325)  评论(0编辑  收藏  举报