Python修改文件内容
2016-07-24 21:18
555 查看
工作中要写个脚本来修改文件的内容,然后就写了一个刷子:
#coding:utf8 import os def modify_file(old_file, new_version, old_version="https"): """ 修改文件内容 """ if os.path.isfile(old_file): try: new_file = "%s.bak" % old_file temp = file(new_file, "wb") with open(old_file, "rb") as f: for line in f.readlines(): temp.write(line.replace(old_version, new_version)) temp.close() os.remove(old_file) os.rename(new_file, old_file) except Exception,e: print e.message return True else: return False def file_list(new_version): """ 获取文件列表 """ filelist = ["D:\当前版本号.txt", "D:\当前版本号.py", r"D:\D盘\version.ini"] try: for f in filelist: modify_file(f.decode("utf8"), new_version) print "修改完成!" except Exception,e: print e.message if __name__ == "__main__": new_version = "ftp" file_list(new_version)
相关文章推荐
- python request属性及方法说明
- 习题20 函数和文件
- 利用python进行哈工程教务系统中的绩点计算
- Python的pip安装
- Python.Calling a JSON API
- 以写代学:python 格式化操作符
- python核心编程第九章练习
- Python安装及环境配置
- python之try-except语句
- python学习之开发小游戏---skier(滑雪的人)
- Python爬虫进阶三之Scrapy框架安装配置
- Python.Extracting Data from JSON
- python读取excel文件
- Python函数第四节
- python核心编程第八章练习
- python核心编程第七章练习
- Python学习笔记20:Python Web编程
- python核心编程作业--正则表达式和图形界面--电影搜索器(电影天堂)
- Python开发柜员机系统
- Python学习笔记19:Python图形用户界面编程