Chinaunix首页 | 论坛 | 博客
  • 博客访问: 112607
  • 博文数量: 49
  • 博客积分: 2612
  • 博客等级: 少校
  • 技术积分: 431
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-01 14:31
个人简介

来来去去

文章分类

全部博文(49)

文章存档

2015年(1)

2012年(4)

2011年(1)

2010年(42)

2009年(1)

我的朋友

分类: Python/Ruby

2010-08-12 11:31:40

''' This script used to rename the file that you specified.
    Usage: rename.py parameter1 parameter2.
    parameter1 is the parent path(dir) of the file that you want to rename.
    parameter2 is the file type which you want to rename.
'
''
import os
import re
import sys

def rename_p(path,file_type):
    path=path
    filenames=os.listdir(path)
    regx="^SPC.*"+"."+file_type+"$"
    #print regx
    
    for f in filenames:
        #print f
        if re.search(regx,f)!=None:
            '''Split the filename and extension'''
            oldname=os.path.splitext(f)[0]
            #print 'The old name is:', oldname
            ext=os.path.splitext(f)[1]
            #print 'The extentsion is:',ext
            #new_name='SPC_'+oldname+'.'+file_type
            new_name=oldname+'.'+'JPEG'
            os.rename(path+'\\'+f,path+'\\'+new_name)
    for f in os.listdir(path):
        print f
     
        
if __name__=='__main__':
    path=sys.argv[1]
    file_type=sys.argv[2]
    rename_p(path,file_type)


阅读(509) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~