python: outfile to another text file if exceed certain file size

file-io, python

Solution

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 ~]$

Problem

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.

Original source

Related problems