storing binary files to mySQL

steindl fritz python at floSoft.org
Fri Dec 20 05:40:12 EST 2002


Fredrik Lundh wrote:

> "steindl fritz" wrote:
> 
>> BUT NOT FOR ME
> 
>> encode(input, output)
>>   Encode a file.
> 
>> encodestring(s)
>>   Encode a string.
> 
>> -------------------------------------------------------
>> import base64
>>
>> file = open("c:\\picture.jpg", 'rb')
>> image = file.read()
>> codedImage = None
>> base64.encode(image, codedImage)
>>
>>
>> Traceback (most recent call last):
>>   File "C:\Python21\o2db\testmysl.py", line 6, in ?
>>     base64.encode(file.read(), codedImage)
> 
> (the text in the exception doesn't match your code sample)


you are absolutely right - 

i tried so many versions ...
(the error stays constant)

------------------------------------------------------------
 
> hints:
> 
> - what does "file.read()" return?  a file or a string?
> - is codedImage a file?

the 'open' command opens a file in textmode (standard) 
or with given parameter 'b' in binary mode

so i would say the result of file.read() depends on the opening method of 
the file

file = open("c:\\picture.jpg", 'rb')
image = file.read()

when i don't open the file in binary mode i miss a lot of bytes compared to 
original filesize
so the correct opening must be with 'b'


type of codedImage ? - i need to define a variable to put the data in and i 
think it gets the type implicit when assigning the first time a value

--------------------------------------------------------------

> maybe the "encodestring" function is more appropriate, given
> the data you have?  does this work:
> 
>     codedImage = base64.encodestring(image)
> 
> </F>

ahh, that works
so there must be an implicit conversion to string in this line
>>> image = file.read() <<<


--------------------------------------------------------------


many thx for your ideas (and patience)


fritz
(-:fs)





More information about the Python-list mailing list