[code.view]

[top] / python / PyMOTW / array / array_file.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2008 Doug Hellmann All rights reserved.
     #
     """
     """
     
     __version__ = "$Id$"
     #end_pymotw_header
     
     import array
     import binascii
     import tempfile
     
     a = array.array('i', xrange(5))
     print 'A1:', a
     
     # Write the array of numbers to the file
     output = tempfile.NamedTemporaryFile()
     a.tofile(output.file) # must pass an *actual* file
     output.flush()
     
     # Read the raw data
     input = open(output.name, 'rb')
     raw_data = input.read()
     print 'Raw Contents:', binascii.hexlify(raw_data)
     
     # Read the data into an array
     input.seek(0)
     a2 = array.array('i')
     a2.fromfile(input, len(a))
     print 'A2:', a2

[top] / python / PyMOTW / array / array_file.py

contact | logmethods.com