python 遍历指定文件夹下的所有文件内容
原创
©著作权归作者所有:来自51CTO博客作者mingjie1212的原创作品,请联系作者获取转载授权,否则将追究法律责任
如果一个一个文件的copy代码 就真是out,最近在学习python,就写了个python的脚本,方便上面操作。
__author__ = 'jghost'
import os
for root,dirs,files in os.walk('your source'):
print root #path
for name in files:
if name.endswith('.java'):
dic = open('/home/jghost/filefilter.txt','a')
dic.write('\n')
dic.write('\n')
dic.write(name+'\n')
dic.write('#author:jgost\n')
dic.write('#company:xxxx\n')
source = open(root+'/'+name)
while True:
context = source.readline()
if context.strip().startswith('#'):
continue
dic.write(context)
if not context:
break
source.close()
dic.close()