python:outfile到另一个文本文件,如果超过一定的文件大小 [英] python: outfile to another text file if exceed certain file size
问题描述
说,我有两个文本文件,这是脚本运行时写,然后我想检查文件是否超过了例如500bytes,如果是,那么它将存储在文件中的任何东西,并创建一个新的文本文件来写。 (output1.txt,output2.txt等)。
如果有任何专业知识将有所帮助,将不胜感激。谢谢
部分代码是:
pre > file = open(output.txt,w)
def示例(p):
if p.haslayer(Dot11Beacon):
如果p.addr2不在uniqueAP中:
file.writelines(p.addr2 +\\\
)
所以当脚本运行在终端时,它会将它写入到名为output.txt的文件中,但是我想要改善脚本以检查文件大小的文本文件,如果超过它将停止写入当前和创建一个新的output2.txt例如,并继续。
一个简单的例子,如果你不想使用 RotatingFileHandler 。
您应该使用 os.stat('filename')。st_size
来检查文件大小。 b
$ b
import os
import sys
class Rotat (文件):
def __init __(self,directory ='',filename ='foo',max_files = sys.maxint,
max_file_size = 50000):
self.ii = 1
self.directory,self.filename =目录,文件名
self.max_file_size,self.max_files = max_file_size,max_files
self.finished,self.fh = False,None
self。 open()
def rotate(self):
如果需要的话旋转文件
if(os.stat(self.filename_template).st_size> self.max_file_size):
self.close()
self.ii + = 1
if(self.ii< = self.max_files):
self.open()
else:
self.close()
self.finished = True
def open(self):
self.fh = open(self.filename_template ,'w')
def write(self,text =):
self.fh.write(text)
self.fh.flush()
self.rotate()
def close(self):
self.fh.close()
@property
def filename_template(self) :
return self.directory + self.filename +_%0.2d%self.ii
$ b $ if if __name __ =='__ main__':
myfile = RotatingFile(max_files = 9)
而不是myfile.finished:
myfile.write('this is a test')
运行这个...
[mpenning @ Bucksnort〜] $ ls -la | grep foo_
-rw-r - r-- 1 mpenning mpenning 50008 Jun 5 06:51 foo_01
-rw -r - r-- 1 mpenning mpenning 50008 Jun 5 06:51 foo_02
-rw-r - r-- 1 mpenning mpenning 50008 Jun 5 06:51 foo_03
-rw-r - r-- 1 mpenning mpenning 50008 Jun 5 06:51 foo_04
-rw -r - r-- 1 mpenning mpenning 50008 Jun 6 06:51 foo_05
-rw-r - r-- 1 mpenning mpenning 50008 Jun 5 06:51 foo_06
-rw-r-- r-- 1 mpenning mpenning 50008 Jun 5 06:51 foo_07
-rw-r - r-- 1 mpenning mpenning 50008 Jun 5 06:51 foo_08
-rw-r - r-- 1 mpenning mpenning 50008 Jun 5 06:51 foo_09
[mpenning @ Bucksnort〜] $
I using scapy with python in ubuntu. I would like to ask if anyone would know how to code the example:
let say I have two text files which are writing while the script is running then I would like to check the file is exceed example 500bytes, if does then it will store whatever in the file and create a new text file to write. (output1.txt, output2.txt,etc..)
Would be appreciated if any expertise will help. Thanks
part of my code is:
file = open("output.txt","w")
def example(p):
if p.haslayer(Dot11Beacon):
if p.addr2 not in uniqueAP:
file.writelines(p.addr2 + "\n")
so while the script is running in the terminal, it will write it into the file called output.txt but i would want to improve the script to check file size of the text file and if exceed it would stop writing in the current and create a new output2.txt for example and continue.
A simple example if you don't want to use RotatingFileHandler.
You should use os.stat('filename').st_size
to check file sizes.
import os
import sys
class RotatingFile(object):
def __init__(self, directory='', filename='foo', max_files=sys.maxint,
max_file_size=50000):
self.ii = 1
self.directory, self.filename = directory, filename
self.max_file_size, self.max_files = max_file_size, max_files
self.finished, self.fh = False, None
self.open()
def rotate(self):
"""Rotate the file, if necessary"""
if (os.stat(self.filename_template).st_size>self.max_file_size):
self.close()
self.ii += 1
if (self.ii<=self.max_files):
self.open()
else:
self.close()
self.finished = True
def open(self):
self.fh = open(self.filename_template, 'w')
def write(self, text=""):
self.fh.write(text)
self.fh.flush()
self.rotate()
def close(self):
self.fh.close()
@property
def filename_template(self):
return self.directory + self.filename + "_%0.2d" % self.ii
if __name__=='__main__':
myfile = RotatingFile(max_files=9)
while not myfile.finished:
myfile.write('this is a test')
After running this...
[mpenning@Bucksnort ~]$ ls -la | grep foo_
-rw-r--r-- 1 mpenning mpenning 50008 Jun 5 06:51 foo_01
-rw-r--r-- 1 mpenning mpenning 50008 Jun 5 06:51 foo_02
-rw-r--r-- 1 mpenning mpenning 50008 Jun 5 06:51 foo_03
-rw-r--r-- 1 mpenning mpenning 50008 Jun 5 06:51 foo_04
-rw-r--r-- 1 mpenning mpenning 50008 Jun 5 06:51 foo_05
-rw-r--r-- 1 mpenning mpenning 50008 Jun 5 06:51 foo_06
-rw-r--r-- 1 mpenning mpenning 50008 Jun 5 06:51 foo_07
-rw-r--r-- 1 mpenning mpenning 50008 Jun 5 06:51 foo_08
-rw-r--r-- 1 mpenning mpenning 50008 Jun 5 06:51 foo_09
[mpenning@Bucksnort ~]$
这篇关于python:outfile到另一个文本文件,如果超过一定的文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!