python rename

!\usr\bin\env python

import sys
import os

def UsePrompt(msg):
#如果省略path,则path为当前路径
print(‘Useage: batchrename.py [path] newfilenames ‘+msg)
#sys.exit()

def BatchRename(path, pattern):
#设置路径
print(path)
os.chdir(path)
fileList = os.listdir(path)
genNum = 0
for fileItem in fileList:
if len(fileItem)<11:
#print(str(len(fileItem)))
continue
fileName = fileItem[7:11]
dotIndex = fileItem.rfind(‘.’)
fileExt = fileItem[dotIndex : ]
fileFullName = fileName + 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]
UsePrompt(‘2’)
elif len(sys.argv)==1:
path = ‘/sdcard/Atest’
pattern = ‘a.3’
UsePrompt(‘1’)
else:
UsePrompt(‘else’)
BatchRename(path, pattern)

if name == ‘main‘:
main()

你可能感兴趣的:(java)