用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打开目录下所有文件的更多相关文章

  1. Python遍历目录下所有文件的最后一行进行判断若错误及时邮件报警-案例

    遍历目录下所有文件的最后一行进行判断若错误及时邮件报警-案例: #-*- encoding: utf-8 -*- __author__ = 'liudong' import linecache,sys ...

  2. Python 打开目录与指定文件

    Python打开外部文件有很多方法, os.popen打开外部程序,但发现只能打开文件所在目录的文件 os.system可打开外部文件os.system(command) command 要执行的命令 ...

  3. Python语言获取目录下所有文件

    #coding=utf-8# -*- coding: utf-8 -*-import osimport sysreload(sys) sys.setdefaultencoding('utf-8') d ...

  4. Python遍历目录下xlsx文件

    对指定目录下的指定类型文件进行遍历,可对文件名关键字进行条件筛选 返回值为文件地址的列表 import os # 定义一个函数,函数名字为get_all_excel,需要传入一个目录 def get_ ...

  5. python遍历目录下所有文件

    # -*- coding:utf-8 -*- import os if __name__ == "__main__": rootdir = '.\data' list = os.l ...

  6. python获取目录下所有文件

    #方法1:使用os.listdir import os for filename in os.listdir(r'c:\\windows'): print filename #方法2:使用glob模块 ...

  7. python拷贝目录下的文件

    #!/usr/bin/env python # Version = 3.5.2 import shutil base_dir = '/data/media/' file = '/backup/temp ...

  8. python对目录下的文件进行 多条件排序

    在进入正题之前,先介绍一下基础知识: 1.sort(),方法:就是对列表内容进行正向排序,直接在原列表进行修改,返回的是修改后的列表 lists =[1, 5, 10, 8, 6]lists.sort ...

  9. python 读取目录下的文件

    参考方法: import os path = r'C:\Users\Administrator\Desktop\file' for filename in os.listdir(path): prin ...

随机推荐

  1. aop+自定义注解

    自定义注解,并且实现,需要两个文件: 自定义注解类: package com.clc.server.annotation; import java.lang.annotation.ElementTyp ...

  2. NEU 1685: All Pair Shortest Path

    题目描述 Bobo has a directed graph G with n vertex labeled by 1,2,3,..n. Let D(i,j) be the number of edg ...

  3. codeforces 679e Bear and Bad Powers of 42

    传送门:http://codeforces.com/contest/679/problem/E 题目意思很清晰,给你一个序列,要求你完成以下三个操作: 1.输出A[i] 2.将[a,b]区间的所有数字 ...

  4. hdu 4463 Outlets(最小生成树)

    题意:n个点修路,要求总长度最小,但是有两个点p.q必须相连 思路:完全图,prim算法的效率取决于节点数,适用于稠密图.用prim求解. p.q间距离设为0即可,最后输出时加上p.q间的距离 pri ...

  5. bzoj2973石头游戏——矩阵乘法

    题目:权限题! 写了一下,但提交不了,先放着吧. 代码如下: #include<iostream> #include<cstdio> #include<cstring&g ...

  6. 微信小程序开发之https从无到有

    本篇不讲什么是https,什么是SSL,什么是nginx 想了解这些的请绕道,相信有很多优秀的文章会告诉你. 本篇要讲的在最短的时间内,让你的网站从http升级到https. 开始教程前再说一句:ht ...

  7. Ruby Proc 和 lambda的共同点和区别

    Proc 和 lambda 的目的是把block {....} 变成类似方法一样的对象,使其不需要重复编写同样的block. Proc 和 lambda 的共同点: 语法类似Proc.new{|n| ...

  8. Streamline Your App with Design Patterns 用设计模式精简你的应用程序

    Back to Design Patterns Streamline Your App with Design Patterns 用设计模式精简你的应用程序 In Objective-C progra ...

  9. ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 17. 基于Claim和Policy的授权 上

    首先补一下昨天没有讲的东西 只有管理员才能访问UserController RoleController都加上这个角色 Cliam 不是管理员角色的用户访问 cliam是name个Value值的键值对 ...

  10. 斯坦福CS231n—深度学习与计算机视觉----学习笔记 课时5

    课时5 数据驱动的图像分类方式:K最邻近与线性分类器(下) 在参数化方法中,我们要做的是构造一个函数,输入一张图片,对每一类输出一个数值.对任意一张图片,我们会计算出它到底属于哪一类.这些参数有时候也 ...