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打开目录下所有文件的更多相关文章
- Python遍历目录下所有文件的最后一行进行判断若错误及时邮件报警-案例
遍历目录下所有文件的最后一行进行判断若错误及时邮件报警-案例: #-*- encoding: utf-8 -*- __author__ = 'liudong' import linecache,sys ...
- Python 打开目录与指定文件
Python打开外部文件有很多方法, os.popen打开外部程序,但发现只能打开文件所在目录的文件 os.system可打开外部文件os.system(command) command 要执行的命令 ...
- Python语言获取目录下所有文件
#coding=utf-8# -*- coding: utf-8 -*-import osimport sysreload(sys) sys.setdefaultencoding('utf-8') d ...
- Python遍历目录下xlsx文件
对指定目录下的指定类型文件进行遍历,可对文件名关键字进行条件筛选 返回值为文件地址的列表 import os # 定义一个函数,函数名字为get_all_excel,需要传入一个目录 def get_ ...
- python遍历目录下所有文件
# -*- coding:utf-8 -*- import os if __name__ == "__main__": rootdir = '.\data' list = os.l ...
- python获取目录下所有文件
#方法1:使用os.listdir import os for filename in os.listdir(r'c:\\windows'): print filename #方法2:使用glob模块 ...
- python拷贝目录下的文件
#!/usr/bin/env python # Version = 3.5.2 import shutil base_dir = '/data/media/' file = '/backup/temp ...
- python对目录下的文件进行 多条件排序
在进入正题之前,先介绍一下基础知识: 1.sort(),方法:就是对列表内容进行正向排序,直接在原列表进行修改,返回的是修改后的列表 lists =[1, 5, 10, 8, 6]lists.sort ...
- python 读取目录下的文件
参考方法: import os path = r'C:\Users\Administrator\Desktop\file' for filename in os.listdir(path): prin ...
随机推荐
- Struts2中Action接收参数
Struts2中Action接收参数的方法主要有以下三种: Struts2中Action接收参数的方法主要有以下三种: 1.使用Action的属性接收参数: a.定义:在Action类中定义属 ...
- Codeforces Round #254 (Div. 2):B. DZY Loves Chemistry
B. DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standa ...
- C# delegate Action<T> lambda表达式
转载以记录:http://blog.csdn.net/educast/article/details/7219854 在使用 Action<T> 委托时,不必显式定义一个封装只有一个参数的 ...
- js对闭包的理解
原文链接http://www.jb51.net/article/24101.htm,讲的很好,清晰明了.
- chdir函数的使用【学习笔记】
#include "apue.h" #include <fcntl.h> int main(void) { ) err_sys("chdir failed&q ...
- HDU3081 Marriage Match II —— 传递闭包 + 二分图最大匹配 or 传递闭包 + 二分 + 最大流
题目链接:https://vjudge.net/problem/HDU-3081 Marriage Match II Time Limit: 2000/1000 MS (Java/Others) ...
- 请问snmp到底是干啥的。
这个事情分两方面来说:首先是路由器这部分.路由器开启SNMP功能之后,它能够对自己的每个接口上的流量有一个统计,当然统计的不单单只有流量.然后路由器把统计到的内容按一定的格式保存起来.这个格式是大家都 ...
- YTU 2453: 我想有套北京的房
2453: 我想有套北京的房 时间限制: 1 Sec 内存限制: 128 MB 提交: 796 解决: 289 题目描述 小原是一个软件工程师,名叫原黛玛,他在北京工作.现在有一套房子,价格200 ...
- I.MX6 sdio 设备注册及识别
/************************************************************************* * I.MX6 sdio 设备注册及识别 * 说明 ...
- June Challenge 2017
A Good Set 分析:水题,选奇数即可 #include "iostream" #include "cstdio" #include "cstr ...