writing serial port data to the gzip file

jim-on-linux inq1ltd at verizon.net
Mon Dec 18 15:03:01 EST 2006




If someone hasn't already commented, 

Aside from any other problems, the file you are 
trying to write to is (opened)?? in the "w" mode. 
Every time a file is opened in the 'w' mode, 
everything in the file is deleted.

If you open a file in the 'a' mode, then 
everything in the file is left untouched and the 
new data is appended to the end of the file.

Your while loop is deleting everything in the file 
on each loop with the 'w' mode.

try, 
vfile = open('vfile', 'a')
rather than 
vfile = open('vfile', 'w')

jim-on-linux
http:\\www.inqvista.com


> while 1:
>     g=gzip.GzipFile("/root/foofile.gz","w")
>     while dataOnSerialPort():
>         g.write(data)
>     else: g.close()



On Sunday 17 December 2006 20:06, Petr Jakes 
wrote:
> I am trying to save data it is comming from the
> serial port continually for some period.
> (expect reading from serial port is 100% not a
> problem) Following is an example of the code I
> am trying to write. It works, but it produce an
> empty gz file (0kB size) even I am sure I am
> getting data from the serial port. It looks
> like g.close() does not close the gz file.
> I was reading in the doc:
>
> Calling a GzipFile object's close() method does
> not close fileobj, since you might wish to
> append more material after the compressed
> data...
>
> so I am completely lost now...
>
> thanks for your comments.
> Petr Jakes
> ==== snippet of the code  ====
> def dataOnSerialPort():
>     data=s.readLine()
>     if data:
>         return data
>     else:
>         return 0
>
> while 1:
>     g=gzip.GzipFile("/root/foofile.gz","w")
>     while dataOnSerialPort():
>         g.write(data)
>     else: g.close()



More information about the Python-list mailing list