Perl's pack/unpack functions
Chris Barker
Chris.Barker at noaa.gov
Mon May 20 16:18:04 EDT 2002
John Hunter wrote:
> Here is an example that writes 2.5 GB of long integers in binary (I
> wanted to test my python large file support build)
>
> import struct
> fd = open('test.out', 'w')
>
> GB = 250000000L # each long is 4 bytes so 250000000 is 1 GB
> for i in xrange( long(2.5*GB) ):
> fd.write( struct.pack('L', i) )
This code should work fine, but you are probably a bit confused about a
C "long" vs, a Python Long Integer. A Python regular integer is a C
long. A Python Long Integer is an unlimited precision integer. For your
example, if you can pack it into a C long, you can store it in a Python
regular integer. Also, you create GB as a long integer, and then convert
the results of 2.5*GB to a long again, unneccesarily. If you tried to
put a number large enough that it required a Python Long into that
struct, you'd get an OverflowError.
-CHB
--
Christopher Barker, Ph.D.
Oceanographer
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
More information about the Python-list
mailing list