Get the entire file in a variable - error

Larry Bates lbates at syscononline.com
Thu Apr 14 14:53:32 EDT 2005


In the future it REALLY helps if you post the code that
you used to open and read the file.  Otherwise we have
to "guess" what you may of done.

That said, I'll try to guess:

1) Your code shows that you instantiate GzipFile class with
mode "w".  I don't know what your data looks like but
normally you would want to use "wb" to handle binary
data.

2) You also need to do the same thing when you try to read
the file:

fp=file(filename, 'rb')

When you don't the first read will read until it encounters
binary bytest that look like a EOF and quit.  When you open
with "b" mode it reads the number of bytes that the
filesytem has stored for the file.

Hope info helps.

Larry Bates


martijn at gamecreators.nl wrote:
> H!
> 
> I'm using a database and now I want to compress a file and put it into
> the database.
> 
> So I'm using gzip because php can open the gzip file's.
> The only problem is saving the file into the database.
> 
> The function below does this:
> - gzip the file [oke]
> - get all the bytes with tst.getvalue() [error]
> I only get the first line.
> 
> I have the same problem when I try it with file.open(), .read().
> 
> "how to get all the binary data in a variable to put that in a database
> LONG field?"
> 
> Thank's
> 
> def compressFILE(sid,filedata):
>     tst = StringIO.StringIO()
> 
>     #tmp
>     zip = gzip.GzipFile(str(sid)+'.gz','w',5,tst)
>     zip.write(filedata)
> 
>     #put every byte in a database
>     print tst.getvalue()
> 
>     zip.close()
>     tst.close()
> 
>     return
> 



More information about the Python-list mailing list