Advertisement
rfmonk

array_file.py

Jan 12th, 2014
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.52 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3.  
  4. import array
  5. import binascii
  6. import tempfile
  7.  
  8. a = array.array('i', xrange(5))
  9. print 'A1:', a
  10.  
  11. # Write the array of numbers to a temp file
  12. output = tempfile.NamedTemporyFile()
  13. a.tofile(output.file)  # must pass an actual file
  14. output.flush()
  15.  
  16. # Read the raw data
  17. with open(output.name, 'rb') as input:
  18.     raw_data = input.read()
  19.     print 'Raw Contents:', binascii.hexlify(raw_data)
  20.  
  21.     # Read the data into an array
  22.     input.seek(0)
  23.     a2 = array.array('i')
  24.     a2.fromfile(input, len(a))
  25.     print 'A2:', a2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement