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

来来去去

文章分类

全部博文(49)

文章存档

2015年(1)

2012年(4)

2011年(1)

2010年(42)

2009年(1)

我的朋友

分类: Python/Ruby

2010-05-24 17:00:51

import os
import re

def rename_p(path,file_type):
    path=path
    filenames=os.listdir(path)
    regx="."+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]  #Get the file extension
            # print 'The extentsion is:',ext
            new_name=oldname+'new'+'.'+file_type
            os.rename(path+'\\'+f,path+'\\'+new_name)
    for f in os.listdir(path):
        print f
        
        

if __name__=='__main__':
    #print 'Please input the dir/path which you want to rename the files in'
    path=raw_input('Please input the dir/path which you want to rename the files in:')
    file_type=raw_input('\nPlease input the file type that you want to rename:')
    if not os.path.isdir(path):
        print path+' is not exist.'
    else:
        rename_p(path,file_type)


阅读(447) | 评论(0) | 转发(0) |
0

上一篇:笔记

下一篇:python compare--filecmp

给主人留下些什么吧!~~