472,809 Members | 3,461 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,809 software developers and data experts.

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('image.gif','rb')
source=handle.read()
handle.close()

if I save the file directly everything is well :
2A)
handle=file('imageDuplicated.gif','wb')
handle.write(source)
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('text.txt','w')
handle.write(source)
handle.close()

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

handle=file('imageDuplicated.gif','wb')
handle.write(source2)
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.

Jun 14 '06 #1
3 18866
ni******@gmail.com schrieb:
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('image.gif','rb')
source=handle.read()
handle.close()

if I save the file directly everything is well :
2A)
handle=file('imageDuplicated.gif','wb')
handle.write(source)
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('text.txt','w')
handle.write(source)
handle.close()

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

handle=file('imageDuplicated.gif','wb')
handle.write(source2)
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.


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.

http://zephyrfalcon.org/labs/python_pitfalls.html

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

Diez
Jun 14 '06 #2
>
Solution: only use binary files, and do the newline-translation yourself
if needed.

Diez


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....

Jun 17 '06 #3
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(binary_file.read())
text_file.write(mytext)
...
mydata = deserialize(text_file.read())

The functions "serialize" and "deserialize" 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.

ni******@gmail.com wrote:
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('image.gif','rb')
source=handle.read()
handle.close()

if I save the file directly everything is well :
2A)
handle=file('imageDuplicated.gif','wb')
handle.write(source)
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('text.txt','w')
handle.write(source)
handle.close()

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

handle=file('imageDuplicated.gif','wb')
handle.write(source2)
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.


Jun 18 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: simon place | last post by:
is the code below meant to produce rubbish?, i had expected an exception. f=file('readme.txt','w') f.write(' ') f.read() ( PythonWin 2.3 (#46, Jul 29 2003, 18:54:32) on win32. ) I got...
4
by: john smith | last post by:
Hi, I have a file format that is going to contain some parts in ascii, and some parts with raw binary data. Should I open this file with ios::bin or no? For example: filename: a.bin number of...
8
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
8
by: siliconwafer | last post by:
Hi All, If I open a binary file in text mode and use text functions to read it then will I be reading numbers as characters or actual values? What if I open a text file and read it using binary...
68
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
14
by: prasadjoshi124 | last post by:
Hi All, I am writing a small tool which is supposed to fill the filesystem to a specified percent. For, that I need to read how much the file system is full in percent, like the output given...
3
by: John Williams | last post by:
I'm writing a stagenography program to experiment with how it works. The algorithm I'm using appears to be producing the correct result...however I'm struggling with the file input. I never...
3
by: masood.iqbal | last post by:
Hi, Kindly excuse my novice question. In all the literature on ifstream that I have seen, nowhere have I read what happens if you try to read a binary file using the ">>" operator. I ran into...
10
by: rory | last post by:
I can't seem to append a string to the end of a binary file. I'm using the following code: fstream outFile("test.exe", ios::in | ios::out | ios::binary | ios::ate | ios::app)...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.