• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
热爱我的热爱
博客园    首页    新随笔    联系   管理    订阅  订阅

第一篇 python 批量修改文件拓展名

 

  第一篇博客,小记录一下。

  今天想用source insight 打开一工程,结果工程文件的扩展名都是.cc文件,工程打不开,看不到,一个一个改又太麻烦,写个脚本改一下吧,

 

 

 

代码
#_*_encoding:utf-8_*_

import os,sys

def replacefile(original, new):

str
= os.getcwd()
for file in os.listdir(str):
if os.path.splitext(file)[1]==original:
temp
= os.getcwd()+os.sep+os.path.splitext(file)[0]+new
file
= os.getcwd()+os.sep+file
print file+"--->"+temp
os.rename(file,temp)

if __name__== '__main__':
# ori = sys.argv[1]
#
new = sys.argv[2]
path = raw_input('Enter path\n')
try:
os.chdir(path)
except WindowsError:
print "路径输入错误"
sys.exit()
ori
= raw_input('Enter ori formate\n')
new
= raw_input('Enter new formate\n')
ori
= '.'+ori
new
= '.'+new
replacefile(ori, new)

 

这样就ok了。

posted @ 2010-10-24 20:50  soniclq  阅读(392)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3