|
From: D-Man <ds...@ri...> - 2001-03-09 02:44:21
|
On Thu, Mar 08, 2001 at 05:09:42PM -0800, John Mudd wrote:
| This should be easy. Reading a binary file in Jython causes changes to
| the data? But it works in Python.
You forgot to mention that you are using a kernel/OS that likes to
mangle your data. ;-)
Use _binary_ mode for writing _binary_ data.
|
|
| $ jython
| Jython 2.0 on javaVM-1.3.0.01 (JIT: null)
| Type "copyright", "credits" or "license" for more information.
| >>> tiffObj = open('john.tif', 'r').read()
| >>> open('junk.tif', 'w').write(tiffObj)
^
This is "text" mode and Windoze likes to replace every \n with \r\n in
such streams. This is what causes your data corruption. Fortunately
on *nix systems the kernel doesn't mangle your streams under the hood.
For other systems use
open( 'junk.tiff' , 'wb' ).write( tiffObj )
^
| >>>
| $ sum -r junk.tif john.tif
| 04308 25 junk.tif
| 16525 25 john.tif
| $ cmp junk.tif john.tif
| junk.tif john.tif differ: char 170, line 1
| $ python
| Python 2.0 (#3, Feb 1 2001, 04:39:53)
| [GCC 2.7.2.3] on hp-uxB
| Type "copyright", "credits" or "license" for more information.
| >>> tiffObj = open('john.tif', 'r').read()
| >>> open('junk.tif', 'w').write(tiffObj)
| >>>
| $ cmp junk.tif john.tif
| $
|
Your jython invocation gives no indication of your platform (I use the
same utilities and prompt on a Win2k box via cygwin), but your CPython
invocation indicates you are using HP-UX. This explains why the code
works in CPython. HP-UX follows the Unix precedent of not mangling
streams simply because you opened it in "text" mode. IMHO there
should only be one mode for opening files -- the app, not the kernel,
should do ALL stream handling, ASCII and binary. In any case, the
proper documented way of writing binary data (such as your TIFF image)
is to use "wb" as the file mode (in Python, C, C++, Java, etc).
HTH,
-D
|