[Tutor] file i.o

Kim Branson kim.branson at csiro.au
Sun Jan 25 02:44:03 EST 2004


hi all,

i'm learning python and to help me do this i'm replacing alot of perl 
scripts i have with python. I'm using mac python 2.3 running on panther 
( it wasn't a clean install of panther) so its possible there are two 
pythons on the system, but it seems to work ok.

i'm unable to write to a file, i can read and print to the shell but 
only empty files are created. for a quick example below, if i import * 
from os i get the following error. not importing os does not give this 
error. Looking at the docs seems to imply the buffer option i presume 
is the missing integer is set to -1 (system by default).


Traceback (most recent call last):
   File "/Users/kbranson/Desktop/test.py", line 6, in -toplevel-
     output = open('numbers','wa')
TypeError: an integer is required
 >>>

from os import *

output = open('numbers','wa')
counter = 0
while (counter<10):
     output.write("%f\n" % (counter))
     print counter
     counter = counter + 1

one other question, what would the python equivalent of this (below) 
be? i.e executing another programs and grabbing the output straight 
into a list. I haven't been able to find out how to do this, but i'm 
sure there is a way.

open (XLOGP, "/usr/local/dock/bin/xlogp dock_nrg.mol2 |");
$xlogp =<XLOGP>;


cheers

Kim




More information about the Tutor mailing list