python之一键序列化命名当前目录

●用法

rename_dir.bat

rename_file.py %1

●rename_file.py脚本的实现

import os,sys
import shutil,string

gCount = 0
prefix = sys.argv[1]

def getList(dirname,pFunc):
    global gCount
    try:
        ls=os.listdir(dirname)
    except:
        print dirname,'is access deny'
    else:
        for file in ls:
            temp = os.path.join(dirname,file)
            if(os.path.isdir(temp)):
                getList(temp,pFunc)
            else:
                gCount = gCount + 1
                pFunc(dirname,file)
                
def renameFile(dirname,file):
    global prefix
    print file
    if file == "." or file == "..":
        return
    ext = file.split(".")[1]
    newFile = "%.2d.%s" % (gCount,ext)
    print file,"--------->",newFile
    os.rename(os.path.join(dirname,file),os.path.join(dirname,newFile))
print "renaming the following files"
print "====================================================="
getList(os.getcwd(),renameFile)


●脚本功能

一键将当前文件夹下的同类型文件名进行命名(实现递增式命名 01.mp4、02.mp4、03.mp4......)

你可能感兴趣的:(Python)