Thanks Beni & Imri,
Now I understand better.
BTW what is the best user documentation for Python? And do you know if
there is good Hebrew documentation?
________________________________
From: [email protected] [mailto:[email protected]] On
Behalf Of Beni Cherniavsky
Sent: Sunday, December 27, 2009 4:32 PM
To: Yitzhak Wiener
Cc: [email protected]
Subject: Re: [Python-il] problem in script
[Imri beat me to it, but replying anyway in case a more detailed answer
helps.]
First question: are you on Windows or Linux/Mac?
On windows, you must open the file as binary, or CR LF will be read as
LF, killing the binary meaning:
f = file('project_release.a', 'rb')
On Linux/Mac, it doesn't matter, but it's good practice to do it anyway
for portability.
Second problem: you use the same file handle for reading and writing.
Normal file object are open either for reading, or writing. Your is
open for reading (the default).
[If this is indeed the problem, tofile() should have given an error.
I'll verify and submit a bug report.]
You could make it work by opennig in 'rb+' mode, and doing a seek.
But a much cleaner approach is opening the file again, now for writing
only:
f = file('project_release2.a', 'wb')
a.tofile(f)
f.close()
[And since Python 2.6, you can simply write:
with file('project_release2.a', 'wb') as f:
a.tofile(f)
which guarantees closing even if an exception happens.
Works for reading too, of course.]
On Sun, Dec 27, 2009 at 15:41, Yitzhak Wiener <[email protected]>
wrote:
Hi Dear People,
As really beginner in Python, I tried to write a simple script
that opens a binary file and replace all appearance of some data with
another data.
It doesn't show me any error message, but file is not modified.
Can you help to understand why?
My script is as follows:
from array import array
a = array('H')
f = file('project_release.a')
a.fromfile( f,100 )
# update the content
for i in a:
if i == 0xFFFF:
a[ a.index(i) ] = 0x7777
#update to new file
a.tofile(f)
Thanks,
Yitzhak
______________________________________________________________________
DSP Group, Inc. automatically scans all emails and attachments
using MessageLabs Email Security System.
_____________________________________________________________________
_______________________________________________
Python-il mailing list
[email protected]
http://hamakor.org.il/cgi-bin/mailman/listinfo/python-il
--
Beni Cherniavsky-Paskin <[email protected]>
______________________________________________________________________
DSP Group, Inc. automatically scans all emails and attachments using
MessageLabs Email Security System.
_____________________________________________________________________
______________________________________________________________________
DSP Group, Inc. automatically scans all emails and attachments using
MessageLabs Email Security System.
____________________________________________________________________________________________________________________
Python-il mailing list
[email protected]
http://hamakor.org.il/cgi-bin/mailman/listinfo/python-il