문제

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?

도움이 되었습니까?

해결책 2

Use subprocess. It was intended to replace spawn.

import subprocess

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

다른 팁

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

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)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top