python遍历文件


 1 #!/usr/local/bin/python
 2 # -*- coding: UTF-8 -*-
 3 #coding:gbk
 4 import re
 5 import os
 6 rootdir = 'src'
 7 
 8 def bianli(str,str2):
 9     list = os.listdir(str) #列出文件夹下所有的目录与文件
10     for i in range(0,len(list)):
11         w_str=""
12         w_str1=""
13         path = os.path.join(str,list[i])
14         print path
15         if os.path.isfile(path):
16             # print path
17             fopen=open(path,'r')
18             for line in fopen:
19                 w_str+=line
20             fopen.close()
21             list1=re.findall(r"public (.+?)\(",w_str)
22             for xxx in list1:
23                 aa=xxx.find('(')
24                 bb=xxx.find(' ')
25                 if (aa<0)and(bb<0):
26                     if len(xxx)>12:
27                         w_str1+=xxx+","
28                         str2.write(w_str1)
29         else:
30             bianli(path,str2)
31 
32 filee = open('yyypublicll.txt','w')
33 bianli(rootdir,filee)
34 filee.close()
 1 #!/usr/local/bin/python
 2 # -*- coding: UTF-8 -*-
 3 #coding:gbk
 4 import re
 5 old_filea=['stage2/main.min.js','stage1/ProtoGJ.min.js','js/default.thm.min.js']
 6 # 生成list
 7 liststring = open('yyypublicll.txt','r')
 8 information=""
 9 for xx in liststring:
10     information+=xx
11 listparams = information.split(",")
12 listparamsqu =list(set(listparams))
13 listparamsqu.sort(key=listparams.index)
14 print (listparamsqu)
15 liststring.close()
16 
17 
18 for x in old_filea:
19     print "打开文件:"+(x)
20     number=1
21     for xxx in listparamsqu:
22         print (str(number)+xxx)
23         fopen=open(x,'r')
24         w_str=""
25         for line in fopen:
26             if re.search(xxx,line):
27                   line=re.sub(xxx,'h'+str(number),line)
28                 w_str+=line
29               else:
30                 # print '1111'
31                 w_str+=line
32         # print w_str
33         wopen=open(x,'w')
34         wopen.write(w_str)
35         fopen.close()
36         wopen.close()
37         number=number+1
 1 #!/usr/local/bin/python
 2 # -*- coding: UTF-8 -*-
 3 #coding:gbk
 4 import re
 5 import os
 6 # 生成list
 7 liststring = open('cc.txt','r')
 8 information=""
 9 number=1
10 for xx in liststring:
11     information+=xx
12 listparams = information.split(",")
13 xinxi=""
14 for xxx in listparams:
15     xinxi=xxx+","+str(number)
16     print (xinxi)
17     number=number+1
18 liststring.close()
19 raw_input(unicode('按回车键退出...','utf-8').encode('gbk'))

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM