FAQ
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()

If needs the binary code i could upload.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101005/361ec58f/attachment.html>

Search Discussions

  • Jonas H. at Oct 5, 2010 at 10:18 pm

    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()
    So where's the problem? That code should work. Anyway, you want to have
    a look at with-statements.

    Jonas
  • Hidura at Oct 5, 2010 at 10:50 pm
    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:

    \xff\\xd8\\xff\\xe0\\x00\\x10JFIF\\x00\\x01\\x01\\x01\\x00H\\x00H\\x00\\x00\\xff\\xdb\\x00C\\x00\\x05\\x03\\x04\\x04\\x04\\x03\\x05\\x04\\x04\\x04\\x05\\x05\\x05\\x06\\x07\\x0c\\x08\\x07\\x07\\x07\\x07\\x0f\\x0b\\x0b\\t\\x0c\\x11\\x0f\\x12\\x12\\x11\\x0f\\x11\\x11\\x13\\x16\\x1c\\x17\\x13\\x14\\x1a\\x15\\x11\\x11\\x18!\\x18\\x1a\\x1d\\x1d\\x1f\\x1f\\x1f\\x13\\x17"$"\\x1e$\\x1c\\x1e\\x1f\\x1e\\xff\\xdb\\x00C\\x01\\x05\\x05\\x05\\x07\\x06\\x07\\x0e\\x08\\x08\\x0e\\x1e\\x14\\x11\\x14\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\x1e\\xff\\xc0\\x00\\x11\\x08\\x05\\xc6\\x04\\\\\\x03\\x01"\\x00\\x02\\x11\\x01\\x03\\x11\\x01\\xff\\xc4\\x00\\x1d\\x00\\x00\\x01\\x05\\x01\\x01\\x01\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x03\\x01\\x02\\x04\\x05\\x06\\x00\\x07\\x08\\t\\xff\\xc4\\x00M\\x10\\x00\\x02\\x01\\x03\\x03\\x02\\x04\\x03\\x06\\x03\\x06\\x04\\x05\\x02\\x01\\r\\x01\\x02\\x03\\x00\\x04\\x11\\x05\\x12!1A\\x06\\x13"Q2aq\\x07\\x14#BR\\x81b\\x91\\xa1\\x153r\\xb1\\xc1\\xd1$\\x82\\xe1\\xf0\\x08\\x164\\x92\\xa2C\\xc2\\xf1%5S\\xb2\\xd2\\xf2c\\xe2DE\\x176s\\xff\\xc4\\x00\\x1a\\x01\\x00\\x03\\x01\\x01\\x01\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\xff\\xc4\\x004\\x11\\x00\\x02\\x02\\x02\\x02\\x02\\x02\\x02\\x00\\x05\\x04\\x01\\x03\\x05\\x01\\x00\\x00\\x01\\x02\\x11\\x03!\\x121\\x04A"2\\x13Q\\x05Baq\\x81\\x14#3\\xf0\\x91\\xb1\\xc1\\xd1CR\\xa1\\xe1\\xf1b\\xff\\xda\\x00\\x0c\\x03\\x01\\x00\\x02\\x11\\x03\\x11\\x00?\\x00\\xf9u\\x99U\\xbd-I\\xbf\\xf8\\xab\\xbf\\xe6\\xa6\\xaf\\xc5\\xf0\\xd6F\\x9d\\x05\\x8eV\\xf8Ui\\xdb\\xe4m\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`\\x1c\\x8a\\\\\\x10\\x8b\\xcf\\xed\\xbfHHm\\x89\\x03\\xf5\\xff\\x00\\xa56M^\\xe8\\xf4\\x8e%n\\xfc\\xe4\\xd5!c\\x8e)C\\x96\\x1c\\xb7J\\xaf\\xc7\\x10\\xb4\\x8bY\\xb5\\x9b\\xd6
    \\x89Uq\\xfc5\\x1eMV\\xe8\\xa7\\xaa`\\xfb\\xbb\\x85\\xe9P\\x0bo8\\xc6~t\\xc78\\x02\\x9a\\x84D\\xd92+\\xc9Y\\xf6\\xc8\\xecs\\xf2\\xc5J\\x88\\xe6<1%\\x83~\\xaa\\xa7\\xdf\\xb4\\xe4U\\x84R\\x1d\\xa0\\xe3\\xb5L\\xd24\\xc5?E\\x92\\\\\\xc8\\xb1\\xacj\\xf8\\xe7\\xf5QM\\xcc\\xfbH\\x12\\x9d\\xb9\\xe3\\x9c\\xd5p\\x93$\\x10:\\nT\\x9c\\x83\\xcde\\xc4\\xe9N/\\xb2\\xc8^\\xdd\\xaac\\xcd\\x1b~\\x94xoG\\x96\\x0b\\xc6\\xac\\xde\\xf5V%\\xc8\\xfa\\xd1a\\x93\\x03?\\xb5KCQ\\x83-~\\xf56\\xd5p\\xca\\xa0t\\x06\\x99%\\xdc\\xa5G\\xe2\\x0c\\xfc\\xaa30)\\xc5\\x05\\xbd%p\\xbe\\xfc\\xd6T\\xce\\xaf\\xc7\\x8d-"J\\\\L\\xa4\\xe2\\xe1\\xc6{\\x06\\xa43\\xca\\x00+!=\\x89&\\xa2\\xab\\xb3\\x11\\x83\\x8aU\\x90\\xf4#&\\x9e\\xc3\\x8c?D\\x89\\x1eBC\\x17\\xe7\\xe5B

    Sorry for the last send.
    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()




    So where's the problem? That code should work. Anyway, you want to have
    a look at with-statements.


    Jonas

    --

    http://mail.python.org/mailman/listinfo/python-list
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20101005/8f1f9b1c/attachment.html>
  • MRAB at Oct 6, 2010 at 12:03 am

    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:
    [snip]

    Sorry for the last send.
    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()




    So where's the problem? That code should work. Anyway, you want to
    have a look at with-statements.
    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.
  • Hidura at Oct 6, 2010 at 4:53 am
    I has to use repr to convert in this string: ????\x00\x10JFIF?
    On Oct 5, 2010 8:03pm, MRAB 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:
    [snip]
    Sorry for the last send.
    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()
    > > >

    > > >

    > > >

    > > >
    So where's the problem? That code should work. Anyway, you want to
    have a look at with-statements.
    > > >

    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.
    --
    http://mail.python.org/mailman/listinfo/python-list
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20101006/fcec3784/attachment.html>
  • Chris Rebert at Oct 6, 2010 at 5:01 am

    On Oct 5, 2010 8:03pm, MRAB 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:
    <snip>
    ?> 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()
    <snip>
    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, 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
  • Hidura at Oct 6, 2010 at 2:25 pm
    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 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/a7f54fe5/attachment.html>
  • Hidura at Oct 6, 2010 at 3:09 pm
    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>
  • MRAB at Oct 6, 2010 at 5:45 pm

    On 06/10/2010 15:25, 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
    [snip]
    You are using Python 3 and type(str) returns "<class 'type'>"?

    Binary data in Python 3 should be an instance of the 'bytes' class, not
    an instance of the 'str' class.

    If you can't fix that, you could turn the string into bytes using:

    data = bytes(ord(c) for c in str)

    or by carefully choosing an encoding which would give the same result:

    data = str.encode('latin-1')

    Then you can save it:

    s = open('/home/hidura/test.jpeg', 'wb')
    s.write(data)
    s.close()

    I asked you to look at the result of repr so that you could see more
    clearly what the data actually looked like, an instance of str or an
    instance of bytes.
  • Hidura at Oct 6, 2010 at 7:13 pm
    Ppl thanyou, for all your help finally i did it!!!!! thanks, another thing
    to i have to send a propouse code, i can fixed the litle problem of the
    wsig.input in Python 3 i will tested in the next months but i want to share
    the code with the comunnity, how i can do that?
    On Oct 6, 2010 1:45pm, MRAB wrote:
    On 06/10/2010 15:25, 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


    [snip]
    You are using Python 3 and type(str) returns ""?
    Binary data in Python 3 should be an instance of the 'bytes' class, not
    an instance of the 'str' class.
    If you can't fix that, you could turn the string into bytes using:
    data = bytes(ord(c) for c in str)
    or by carefully choosing an encoding which would give the same result:
    data = str.encode('latin-1')
    Then you can save it:
    s = open('/home/hidura/test.jpeg', 'wb')
    s.write(data)
    s.close()
    I asked you to look at the result of repr so that you could see more
    clearly what the data actually looked like, an instance of str or an
    instance of bytes.
    --
    http://mail.python.org/mailman/listinfo/python-list
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20101006/9d3762bf/attachment.html>
  • Hidura at Oct 6, 2010 at 7:15 pm
    Ppl thanyou, for all your help finally i did it!!!!! thanks, another thing
    to who i can send a propose code, i fixed the little problem of the
    wsig.input in Python 3 i will tested in the next months but i want to share
    the code with the community, how i can do that?
    On Oct 6, 2010 3:13pm, hidura at gmail.com wrote:
    Ppl thanyou, for all your help finally i did it!!!!! thanks, another
    thing to i have to send a propouse code, i can fixed the litle problem of
    the wsig.input in Python 3 i will tested in the next months but i want to
    share the code with the comunnity, how i can do that?
    On Oct 6, 2010 1:45pm, MRAB python at mrabarnett.plus.com> wrote:
    On 06/10/2010 15:25, 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




    [snip]

    You are using Python 3 and type(str) returns ""?



    Binary data in Python 3 should be an instance of the 'bytes' class, not

    an instance of the 'str' class.



    If you can't fix that, you could turn the string into bytes using:



    data = bytes(ord(c) for c in str)



    or by carefully choosing an encoding which would give the same result:



    data = str.encode('latin-1')



    Then you can save it:



    s = open('/home/hidura/test.jpeg', 'wb')

    s.write(data)

    s.close()



    I asked you to look at the result of repr so that you could see more

    clearly what the data actually looked like, an instance of str or an

    instance of bytes.

    --

    http://mail.python.org/mailman/listinfo/python-list
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20101006/94affedb/attachment.html>
  • Hidura at Oct 10, 2010 at 4:46 am
    Finally i had problems to save the files what are encoded i can't encode
    the string to save the file, any ideas?
    On Oct 6, 2010 3:15pm, hidura at gmail.com wrote:
    Ppl thanyou, for all your help finally i did it!!!!! thanks, another
    thing to who i can send a propose code, i fixed the little problem of the
    wsig.input in Python 3 i will tested in the next months but i want to
    share the code with the community, how i can do that?
    On Oct 6, 2010 3:13pm, hidura at gmail.com wrote:
    Ppl thanyou, for all your help finally i did it!!!!! thanks, another
    thing to i have to send a propouse code, i can fixed the litle problem of
    the wsig.input in Python 3 i will tested in the next months but i want to
    share the code with the comunnity, how i can do that?
    On Oct 6, 2010 1:45pm, MRAB python at mrabarnett.plus.com> wrote:
    On 06/10/2010 15:25, 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




    [snip]

    You are using Python 3 and type(str) returns ""?



    Binary data in Python 3 should be an instance of the 'bytes' class,
    not
    an instance of the 'str' class.



    If you can't fix that, you could turn the string into bytes using:



    data = bytes(ord(c) for c in str)



    or by carefully choosing an encoding which would give the same result:



    data = str.encode('latin-1')



    Then you can save it:



    s = open('/home/hidura/test.jpeg', 'wb')

    s.write(data)

    s.close()



    I asked you to look at the result of repr so that you could see more

    clearly what the data actually looked like, an instance of str or an

    instance of bytes.

    --

    http://mail.python.org/mailman/listinfo/python-list
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20101010/d94df2ef/attachment.html>
  • Related Discussions

    Discussion Navigation
    viewthread | post
    Discussion Overview
    grouppython-list @
    categoriespython
    postedOct 5, '10 at 9:11p
    activeOct 10, '10 at 4:46a
    posts12
    users4
    websitepython.org

    People

    Translate

    site design / logo © 2022 Grokbase