首页 > 代码库 > Python打开目录下所有文件
Python打开目录下所有文件
用Python打开指定目录下所有文件,统计文件里特定的字段信息。
这里是先进入2017-02-25到2017-03-03目录,然后进入特定IP段目录下,最后打开文件进行统计
import os, glob, re, sys from isCN import * urllocate = 6 if(len(sys.argv) < 3): print ‘Useage comand JDName JDIPduan‘; os.exit() print sys.argv[1]; ipDuan = sys.argv[2] for i in range(1, 8+1): singleStream = 0 in2out = 0 lineNum = 0 if(i >= 1 and i <= 3 ): day = ‘2017-03-0‘ + str(i) if(i >= 5 and i <= 8 ): day = ‘2017-02-2‘ + str(i) if(i == 4): continue try: os.chdir(‘./‘ + day) except: continue files = glob.glob(ipDuan) for f in files: print f; try: f = open(‘./‘ + f + ‘/find_prog.log‘) except: continue #print f; while True: line = f.readline() if not line: break lineNum = lineNum + 1 #print line, fields = line.split(‘\t‘) length = len(fields) #print ‘length=‘+str(length); if(length >= urllocate): url = fields[urllocate-1] url = url.strip() #print url, if(url==‘null‘): singleStream = singleStream + 1 try: cipPort = fields[1].split(‘-‘)[0] cip = cipPort[0 : cipPort.rindex(‘.‘)] #print cip; if(iscn(cip) == True): in2out = in2out + 1 except: continue line = f.readline() f.close() #print ‘single=‘ + str(singleStream); #print ‘lineNum=‘ + str(lineNum); print day + ‘ cn2foreign = ‘ + str(in2out) + ‘/‘ + str(lineNum) + ‘ = ‘ + str(1.0 * in2out / lineNum) + ‘\n‘; print day + ‘ singleStream = ‘ + str(singleStream) + ‘/‘ + str(lineNum) + ‘ = ‘ + str(1.0 * singleStream / lineNum) + ‘\n‘;
其中用到了别人的库,用于判断IP是否属于国内IP
python JDStatistic.py JDName 10.*
Python打开目录下所有文件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。