码迷,mamicode.com
首页 > 编程语言 > 详细

Python打开目录下所有文件

时间:2017-03-12 15:17:50      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:code   判断   foreign   style   stream   index   use   sys   locate   

用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打开目录下所有文件

标签:code   判断   foreign   style   stream   index   use   sys   locate   

原文地址:http://www.cnblogs.com/vincent-vg/p/6537784.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!