python批量修改文件脚本

时间:2015-05-15 13:34:46   收藏:0   阅读:138

最近准备把《ThingingInJava》敲一遍(人比较笨,只能用这种简单粗暴的方法了,哈哈)

必须要把net.mindview包导入

导入过程参考这篇博客:http://blog.sina.com.cn/s/blog_631a75580100vcig.html

中间由于要把所有文件中的com.mindview删掉

就写了一个简陋的python脚本,批量删除

由于这是第一次为了自己方便写脚本(这可能也是脚本的意义),纪念一下

下面是代码:

import re
import os

def hand1(curdir):
	for file_name in os.listdir(curdir):
		print file_name
		if(file_name!=‘hand.py‘):
			hand2(file_name)


def hand2(file_name):
	fp = open(file_name,‘r‘)
	alllines=fp.readlines()
	fp.close
	fp=open(file_name,‘w‘)
	for eachline in alllines:
		a=re.sub(‘net.mindview.‘,‘ ‘,eachline)
		fp.writelines(a)
	fp.close
	
if __name__=="__main__":
	hand1(os.path.abspath(‘.‘))

  

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!