python 批量更改後綴名

#-*-coding:utf-8-*-

import os

if __name__=='__main__':
    old_ext=input('Please input the old ext:')
    new_ext=input('Please input the new ext:')
    for (path,dirs,filenames) in os.walk('/root/Programs'):
        print 'jump in to the first for/n'
        for filename in filenames:
            ext=os.path.splitext(filename)[1]
            print ext
            if(cmp(old_ext,ext)==0):
                new_name=filename.replace(ext,new_ext)
                old_path=path+'/'+filename
                new_path=path+'/'+new_name
               
                print 'the old_path is'+old_path+'/n'
                print 'the new_path is'+new_path+'/n'
       
                os.rename(old_path,new_path)

 

 

python popenpython os模塊裏的一個方法,它用來執行一些系統的命令

例如:

for i in os.popen('ls'):
      print i

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章