21.8.08

Python tips

Notes on my struggle with Python. :)

1. Os.system call and wildcards.

You might have a hard life trying to send commands with wildcards to shell using "os.system()" call. If it treats wildcards as a part of the filename, use module "subprocess", for example:

>>> subprocess.check_call('ls /data/sat/noaa/l1d/*l1d*', shell=True)


2. Writing a binary file with python
import struct
n = 1066
bfile.write(struct.pack('i',n))

Or

import array
numbers = array.array('i',[1066,1035,1032,1023])
bfile.write(numbers.tostring())

Or

import Numeric
numbers = Numeric.array([1,2,3,4,5,6],'i')
bfile.write(numbers.tostring())

Found it here.

No comments: