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 ...
随机推荐
- php网站前台utf-8格式有时会出现莫名其妙的空白行,重新保存下编码格式就可以了
php网站前台utf-8格式有时会出现莫名其妙的空白行,重新保存下编码格式就可以了.
- eclipse、idea切换大小写的快捷键
idea : ctrl+shift+U切换大小写 eclipse : ctrl+shift+X 切换成大写 ctrl+shift+Y 切换成小写
- Oracle安装:silent安装方式
之前一直是通过图形界面来安装oracle,这次上机考试说用silent (静默)形式安装.一点头绪都没有,虽然当时提供了oracle官方文档. 遂查找资料,安装了一下: 一.准备工作: 1.系统参数调 ...
- 探索Oracle11gR2 之 DataGuard 三种保护模式
Oracle的DataGuard技术有三种实现模式,分别是max performance.max availability.maxprotection这三种模式. 以下是来自Oracle文档的摘要信息 ...
- 微信小程序在线制作 自己制作微信小程序
小程序是个什么东西?怎么自己制作微信小程序?微信小程序在线制作难吗?最近老是听这类问题,耳朵都长茧子了. 百牛信息技术bainiu.ltd整理发布于博客园 接下来作为一个技术人员的角度就为大家分析一下 ...
- JavaScript-Tool:wdtree
ylbtech-JavaScript-Tool:wdtree 1.返回顶部 1. 插件描述:wdTree是一个轻量级jQuery插件用于创建一个带有嵌套Check Boxe的树形控件. wdTree是 ...
- View Programming Guide for iOS ---- iOS 视图编程指南(五)---Animations
Animations Animations provide fluid visual transitions between different states of your user inter ...
- Ubuntu16.04 开机显示The system is running in low-graphics mode
1. 在"The system is running in low-graphics mode"界面,直接按 ctrl+alt+F1,会进入一个命令输入的模式 2. 输入用户名密码 ...
- 任务36:应用Jwtbearer Authentication
任务36:应用Jwtbearer Authentication D:\MyDemos\jesse 新建项目:dotnet new webapi --name JwtAuthSample VS2017运 ...
- 用动态链表high-poj 1528
//2333333 题目超级水,但是!刚学了链表拿来high一high也不错啊. #include <iostream> #include <stdio.h> #include ...