File read and writing in binary mode...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nicolasg@gmail.com

    #1

    File read and writing in binary mode...

    Hi,

    I'm trying to open a file (any file) in binary mode and save it inside
    a new text file.
    After that I want to read the source from the text file and save it
    back to the disk with its original form. The problem is tha the binary
    source that I extract from the text file seems to be diferent from the
    source I saved. Here is my code:
    1)
    handle=file('im age.gif','rb')
    source=handle.r ead()
    handle.close()

    if I save the file directly everything is well :
    2A)
    handle=file('im ageDuplicated.g if','wb')
    handle.write(so urce)
    handle.close()

    the file imageDuplicated .gif will be exactly the same as the original
    image.gif.
    But if I save the source to a text file I have porblem :
    2B)
    handle=file('te xt.txt','w')
    handle.write(so urce)
    handle.close()

    handle=file('te xt.txt','r')
    source2=handle. read()
    handle.close()

    handle=file('im ageDuplicated.g if','wb')
    handle.write(so urce2)
    handle.close()

    the files are completly different and I even cant display the image
    from the imageDuplicated .gif .

    something changes when I save the source in the text file because in
    2B) source == source2 returns a False .
    I suspect that maybe the encoding is making a conflict but I don't know
    how to manipulate it...
    Every help is welcome, thanks.

  • Diez B. Roggisch

    #2
    Re: File read and writing in binary mode...

    nicolasg@gmail. com schrieb:[color=blue]
    > Hi,
    >
    > I'm trying to open a file (any file) in binary mode and save it inside
    > a new text file.
    > After that I want to read the source from the text file and save it
    > back to the disk with its original form. The problem is tha the binary
    > source that I extract from the text file seems to be diferent from the
    > source I saved. Here is my code:
    > 1)
    > handle=file('im age.gif','rb')
    > source=handle.r ead()
    > handle.close()
    >
    > if I save the file directly everything is well :
    > 2A)
    > handle=file('im ageDuplicated.g if','wb')
    > handle.write(so urce)
    > handle.close()
    >
    > the file imageDuplicated .gif will be exactly the same as the original
    > image.gif.
    > But if I save the source to a text file I have porblem :
    > 2B)
    > handle=file('te xt.txt','w')
    > handle.write(so urce)
    > handle.close()
    >
    > handle=file('te xt.txt','r')
    > source2=handle. read()
    > handle.close()
    >
    > handle=file('im ageDuplicated.g if','wb')
    > handle.write(so urce2)
    > handle.close()
    >
    > the files are completly different and I even cant display the image
    > from the imageDuplicated .gif .
    >
    > something changes when I save the source in the text file because in
    > 2B) source == source2 returns a False .
    > I suspect that maybe the encoding is making a conflict but I don't know
    > how to manipulate it...
    > Every help is welcome, thanks.[/color]

    Now why do you think there is a distinction between binary and text
    files? Precisely because of what you observe: a text file will undergo a
    automatice file ending conversion. That means that newlines get
    translated to DOS-newlines (actually two characters) - and that makes a
    binary file corrupted.



    Solution: only use binary files, and do the newline-translation yourself
    if needed.

    Diez

    Comment

    • nicolasg@gmail.com

      #3
      Re: File read and writing in binary mode...

      >[color=blue]
      > Solution: only use binary files, and do the newline-translation yourself
      > if needed.
      >
      > Diez[/color]

      The probelm is if I can't use only binary files...
      How can I do the newline-translation myself ? if check the text and
      found the diferrence between binary and text is the '\r' instead of
      '\'n' . I can't change every '\n' because it will change the real '\n'
      ones....

      Comment

      • hdante

        #4
        Re: File read and writing in binary mode...

        Hi,

        I'm sorry, but you have a conceptual error there. Text files differ
        from binary files because they are not considered raw. When you say
        that this or that file is a text file, python and/or the operating
        system takes the liberty to insert and or remove semantics from the
        file, according to some formatting agreed/imposed by them. Text files
        are formatted files. Binary files are raw files. You can't expect by
        any means that python will respect your raw data when writing text
        files.

        The solution to the question "how can I write a binary file into a
        text file" requires that you convert the binary file to a format
        suitable for textual access. For example, you can "uuencode" the binary
        file inside your text file. In simple terms:

        mytext = serialize(binar y_file.read())
        text_file.write (mytext)
        ...
        mydata = deserialize(tex t_file.read())

        The functions "serialize" and "deserializ e" are responsible for
        converting the binary data to/from some textual representation.

        HOWEVER, why would you want to put binary data into a text file ? Is
        this some information that will be used by your application ? Or will
        you transfer it to some other person in a portable way ? Maybe you
        should leave those files alone and not try to merge them. If it is a
        complex structure you should put it into a database instead of doing
        those strange things. In the worst case, you could just write a text
        file, write a binary file and concatenate them later. See if this
        really is a requirement for your project.



        nicolasg@gmail. com wrote:[color=blue]
        > Hi,
        >
        > I'm trying to open a file (any file) in binary mode and save it inside
        > a new text file.
        > After that I want to read the source from the text file and save it
        > back to the disk with its original form. The problem is tha the binary
        > source that I extract from the text file seems to be diferent from the
        > source I saved. Here is my code:
        > 1)
        > handle=file('im age.gif','rb')
        > source=handle.r ead()
        > handle.close()
        >
        > if I save the file directly everything is well :
        > 2A)
        > handle=file('im ageDuplicated.g if','wb')
        > handle.write(so urce)
        > handle.close()
        >
        > the file imageDuplicated .gif will be exactly the same as the original
        > image.gif.
        > But if I save the source to a text file I have porblem :
        > 2B)
        > handle=file('te xt.txt','w')
        > handle.write(so urce)
        > handle.close()
        >
        > handle=file('te xt.txt','r')
        > source2=handle. read()
        > handle.close()
        >
        > handle=file('im ageDuplicated.g if','wb')
        > handle.write(so urce2)
        > handle.close()
        >
        > the files are completly different and I even cant display the image
        > from the imageDuplicated .gif .
        >
        > something changes when I save the source in the text file because in
        > 2B) source == source2 returns a False .
        > I suspect that maybe the encoding is making a conflict but I don't know
        > how to manipulate it...
        > Every help is welcome, thanks.[/color]

        Comment

        Working...