pyrename with regex

改进版支持正则批量修改文件名

#!/usr/bin/python2

#auth:codewalker
#mail:001@codewalker.me
#version:0.2

from sys import argv, exit
from os import rename, listdir, getcwd
import re


if len(argv) ==  3:
    try:
        files = listdir(getcwd())
        for old_file_name in files:
            if argv[1] in old_file_name:
                new_file_name = re.sub(argv[1],argv[2],old_file_name)
                rename(old_file_name,new_file_name) #change names
                print '%s => %s' % (old_file_name, new_file_name)
    except OSError,e:
        print e
    else: print "DONE"
else:

    print "USAGE:%s oldpart newpart" % argv[0]

    exit(1)

发表评论

电子邮件地址不会被公开。 必填项已用*标注