How to save a binary file?

hidura at gmail.com hidura at gmail.com
Wed Oct 6 11:09:49 EDT 2010


How i get the code what MRAB says theres any manual or example?, because i  
was trying in Python2.x to do it and give me the same mistake, when i try  
to use repr that give me more backslash

On Oct 6, 2010 10:25am, hidura at gmail.com wrote:
> When you put the 'wb' extension you have to pass a Encode the string  
> Python does not accept a string on a wb file, Python3

> On Oct 6, 2010 1:01am, Chris Rebert clp2 at rebertia.com> wrote:
> > > On Oct 5, 2010 8:03pm, MRAB python at mrabarnett.plus.com> wrote:
> >
> > >> On 05/10/2010 23:50, hidura at gmail.com wrote:
> >
> > >> I did but the mistake is: Error interpreting JPEG image file (Not a  
> JPEG
> >
> > >> file: starts with 0x5c 0x6e)
> >
> > >>
> >
> > >> I think the problem is maybe in the binary code here is:
> >
> >
> >
> > >> > On Oct 5, 2010 6:18pm, "Jonas H." jonas at lophus.org> wrote:
> >
> > >> > > On 10/05/2010 11:11 PM, hidura at gmail.com wrote:
> >
> > >> > > Hello, how i can save a binary file, i read in the manual in the  
> IO
> >
> > >> area
> >
> > >> > > but doesn' t show how to save it.
> >
> > >>
> >
> > >> > > Here is the code what i am using:
> >
> > >> > > s = open('/home/hidura/test.jpeg', 'wb')
> >
> > >> > > s.write(str.encode(formFields[5]))
> >
> > >> > > s.close()
> >
> >
> >
> > >> Why are you encoding it? A JPEG file should contain the binary data,
> >
> > >> not a textual encoding of its bytes. The error message you got said
> >
> > >> that the contents of the file started with a backslash.
> >
> > >>
> >
> > >> If you print out, say, repr(str[ : 10]) you should get something like
> >
> > >>
> >
> > >> 'ÿØÿà\x00\x10JFIF'.
> >
> > >>
> >
> > >> Try this instead:
> >
> > >>
> >
> > >> s = open('/home/hidura/test.jpeg', 'wb')
> >
> > >> s.write(str)
> >
> > >> s.close()
> >
> > >>
> >
> > >> Incidentally, 'str' is a bad name for a variable because it's the  
> name
> >
> > >> of the built-in string type.
> >
> >
> >
> > On Tue, Oct 5, 2010 at 9:53 PM, hidura at gmail.com> wrote:
> >
> > > I has to use repr to convert in this string: ÿØÿà\x00\x10JFIF?
> >
> >
> >
> > No, you don't/shouldn't convert it at all. Read/use the 3 lines of
> >
> > suggested code that MRAB gave:
> >
> > s = open('/home/hidura/test.jpeg', 'wb')
> >
> > s.write(str)
> >
> > s.close()
> >
> >
> >
> > Cheers,
> >
> > Chris
> >
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101006/4bbb2350/attachment-0001.html>


More information about the Python-list mailing list