1 #!/usr/bin/env python 2 #coding=utf-8 3 import os,sys 4 import shutil,re 5 6 def getparam(str): 7 file_re=re.compile(r'(\w*)(#{1,})(\w*)') 8 m=file_re.search(str) 9 return m.groups 10 11 12 def changename(dir): 13 filenum=0 14 i=int(sys.argv[3]) 15 list=os.listdir(dir) 16 for line in list: 17 if os.path.isfile: 18 oldfullfile = dir+'\\'+line 19 k=getparam(sys.argv[2]) 20 n=len(k(1)[1]) 21 ext = oldfullfile.split('.', 1)[1] #get file_extension 22 print('ext: '+ext) 23 newfile=k(1)[0]+"{0:0{1}}".format(i,n)+k(1)[2]+'.'+ext #format string filled by 0 24 print(newfile) 25 newfullfile = dir+'\\'+newfile 26 print(oldfullfile) 27 print(newfullfile) 28 shutil.move(oldfullfile,newfullfile) 29 print(line) 30 i=i+1 31 32 33 if len(sys.argv)!=4: 34 print("arg count error") 35 else: 36 #print(sys.argv[0]) #get python-name 37 #print(sys.argv[2]) 38 k=getparam(sys.argv[2]) 39 changename(sys.argv[1]) 40 #print("{0:04}".format(5))
运行如:e:\python30\python BatchRename.py e:\myPython\rename s####d 1
将把e:\myPython\rename下的文件都改为名为
s0001.py
s0002.py
........
最大
s9999.py
的形式