writing serial port data to the gzip file

Petr Jakes mcbooczech at gmail.com
Mon Dec 18 17:19:21 EST 2006


Maybe I am missing something. Expect data is comming continually to the
serial port for the period say 10min. (say form the GPS), than it stops
for 1 minute and so on over and over. I would like to log such a data
to the different gzip files.
My example was written just for the simplicity (I was trying to
demonstrate the problem, it was not the real code and I was really
tired trying to solve it by myself, sorry for the bugy example)

the better way how to write such a infinite loop can be probably:
===== 8< =====
g=0
x=0
while 1:
    if not g:
        x+=1
        g=gzip.GzipFile("/root/foofile%s.gz" % x,"w")
    data=dataOnSerialPort()
    while data:
        myFlag=1
        g.write(data)
        data=dataOnSerialPort():
    else:
        if myFlag:
            g.close()
            pring g
            myFlag=0

But it looks like g.close() method does not close the file (while
trying to print the g object, it  still exists)


> Your while loop is discarding result of dataOnSerialPort, so you're
> probably writing empty string to the file many times. Typically this
> kind of loop are implemented using iterators. Check if your s object
> (is it from external library?) already implements iterator. If it does
> then
>
> for data in s:
>     g.write(data)
>
> is all you need. If it doesn't, you can use iter to create iterator for
> you:
> 
> for data in iter(s.readLine, ''):
>     g.write(data)
> 
>   -- Leo




More information about the Python-list mailing list