Skip to content Skip to sidebar Skip to footer

Python: Outfile To Another Text File If Exceed Certain File Size

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 t

Solution 1:

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

classRotatingFile(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()

    defrotate(self):
        """Rotate the file, if necessary"""if (os.stat(self.filename_template).st_size>self.max_file_size):
            self.close()
            self.ii += 1if (self.ii<=self.max_files):
                self.open()
            else:
                self.close()
                self.finished = Truedefopen(self):
        self.fh = open(self.filename_template, 'w')

    defwrite(self, text=""):
        self.fh.write(text)
        self.fh.flush()
        self.rotate()

    defclose(self):
        self.fh.close()

    @propertydeffilename_template(self):
        return self.directory + self.filename + "_%0.2d" % self.ii

if __name__=='__main__':
    myfile = RotatingFile(max_files=9)
    whilenot 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~]$

Solution 2:

Another way (a bit shorter) would be to use a with clause having an if condition checking for the file size:-

defRotateFile(count, file_size, file_name):
    indX = 0while indX < count:  
        withopen(file_name,"a") as fd:  
            ##
            fd.write("DUMMY TEXT\n")      
            ##  ifint(os.path.getsize(file_name)) > file_size:
                indX += 1
                file_name = "new_file"+str(indX)+".txt"

with statement creates a context manager, so by the time you reach the end of if condition , the file object in fd will have been automatically closed.

Next is to specify the options you need and pass on to the method:

if __name__ == "__main__":
    count = 10                           ## 10 number of files
    file_size = 10000                    ## 10KB in size
    file_name = "new_file.txt"## with this name
    RotateFile(count, file_size, file_name)

After execution, it will give 10 files of size 10KB each (in the current dir)...

enter image description here

Post a Comment for "Python: Outfile To Another Text File If Exceed Certain File Size"