Scrolling through a file and splitting its contents into separate files using Python

1
from scapy.all import *

pkts = rdpcap("lalalao2.pcap")


#time slice de t em segundos : 10 minutos

t = 600
somaMin = pkts[0].time + t


valores=[]
for p in pkts:

    if p<=somaMin:

        valores.append(p)

    else:


        primslice =valores

        f=open("time1.txt",'w')
        f.writelines(primslice)

        valores=[]

        valores.append(p)

        somaMin=p.time + t

The above code opens a network dump file (PCAP) and creates vectors containing time slices of t seconds. I would like each slice time to be written to a different text file: time1, time2, ... The problem is that I do not know how many slices I have, because it depends on the capture. Is it possible?

    
asked by anonymous 05.03.2017 / 13:52

1 answer

2

Yes, just make the file name variable. Something like open('time{}.txt'.format(i), 'w') where i is a counter.

from scapy.all import *

pkts = rdpcap("lalalao2.pcap")

i = 1
# ^--- Inicia o contador

#time slice de t em segundos : 10 minutos

t = 600
somaMin = pkts[0].time + t

valores=[]
for p in pkts:

    if p<=somaMin:

        valores.append(p)

    else:

        primslice =valores

        f=open("time{}.txt".format(i),'w')
        #           ^--- Deixa o nome do arquivo variável

        f.writelines(primslice)

        i += 1
        # ^--- Incrementa o valor de i indefinidamente enquanto necessário

        valores=[]

        valores.append(p)

        somaMin=p.time + t

This code will generate the files time1.txt , time2.txt , time3.txt , etc.

Note : In your logic, if the number of slices is not exactly a multiple of the time, you will lose the last slice (it will not be written to a file). Imagine that in each slice 100 records will be written, but in the end, in the last slice, not 100 exactly, but only 90. When looping, all records will enter% if_of%, but none will enter% , where the file is written. So I recommend that you convert the write to file into a function, call it inside the else, if you still want it, but after the for, check if there are elements in p<=somaMin and if yes, call the function again.

    
05.03.2017 / 14:30