#-*-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 popen 是python os模块里的一个方法,它用来执行一些系统的命令
例如:
for i in os.popen('ls'):
print i
本文介绍了一个使用Python脚本批量修改文件扩展名的方法。通过简单的输入旧扩展名和新扩展名,该脚本可以在指定目录及其子目录下查找并替换所有匹配的文件扩展名。
717

被折叠的 条评论
为什么被折叠?



