Anybody can help on this?

Jeff Shannon jeff at ccvcorp.com
Thu Feb 21 14:17:19 EST 2002


husam wrote:

> Since I'm a newbie to python, I thought it must
> be possible to automate this process.

Yes, this is a great use for Python.  :)


> I wrote this little script which
> does the job nicely:
>
> listit=os.popen('ls *pdb')

> pdbs=listit.readlines()
> listit.close()

This would probably be better done as

import glob
pdbs = glob.glob('*pdb')

(This will build your file list with significantly less overhead.)


> The problem is how can I save the output to a file?

If you use the popen2() function, instead of popen(), you'll get two file
objects back, one of which is stdin and the other is stdout.

passin, passout = os.popen2('/home/chemistry/profit ')

Now, you can call

passin.write( 'your commands here' )

For getting output, you can take a couple of approaches.  If you just need
to archive everything that is output by your program, a simple

outfile = open('myoutput.txt', 'w')
outfile.write( passout.read() )

should suffice.  You can also read a single line of output at a time ( with
passout.readline() ), but be aware that this call won't return until there's
either something to read, or the pipe closes (i.e., your piped program
terminates), in which latter case it will return an empty string ( '' ).

Jeff Shannon
Technician/Programmer
Credit International






More information about the Python-list mailing list