Jagoda
Jagoda

Reputation: 424

Opening an external program

I'm the Python beginner and I have a task to do. I have to write a function, that opens a program (.bin), execute it so I can see the results. This program requires 2 arguments from command line. I used os.spawnv, but it doesn't work...

#!/usr/bin/python

import sys
import os

def calculate_chi():
    if len(sys.argv)>1:
        pdb_name=sys.argv[1]
        dat_name=sys.argv[2]
    crysol='/usr/bin/crysol'
    os.spawnv(os.P_NOWAIT,crysol,[crysol,pdb_name,dat_name])

def main():
    calculate_chi()

Can you help me?

Upvotes: 0

Views: 190

Answers (3)

venpa
venpa

Reputation: 4318

You can use python subprocess module:

import subprocess
proc = subprocess.Popen(['/usr/bin/crysol', sys.argv[1], sys.argv[2]], shell=True,     stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while proc.poll() is None:
    out = proc.stdout.readline() #read crystol's output from stdout and stderr
    print out  
retunValue = proc.wait() #wait for subprocess to return and get the return value

Upvotes: 1

Jayanth Koushik
Jayanth Koushik

Reputation: 9884

Use subprocess. It was intended to replace spawn.

import subprocess

subprocess.call([crysol, pdb_name, dat_name])

Upvotes: 0

Jakob Bowyer
Jakob Bowyer

Reputation: 34688

Everyone uses subprocess.Popen these days. An example call to your process would be

process = Popen(["/usr/bin/crysol", pdb_name, dat_name], 
                 stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Upvotes: 0

Related Questions