如何使用子流程将命令传递给另一个程序?

时间:2019-06-03 00:51:33

标签: python subprocess algebra

我编写了一个程序,该程序将子程序GAP(分组代数和编程)打开,并在GAP中定义了四个功能。现在,我想将在python中生成的命令传递给此GAP子进程,让GAP评估命令,然后将输出返回给python。

我可以让GAP从文件中读取函数,但是我不能让它实际读取我作为输入提供给它的任何东西。

我尝试过:

process.communicate(file path containing commands)
process.communicate(string of all commands)
process.stdin.write(String of a single command)
process.stdin.write(string of all commands)
GAPpath = r'C:/gap-4.10.1/bin/gap.bat'
    path = os.getcwd()
    mainpath = path.replace(os.sep, '/')
    with open((mainpath +'/deltasetsinit.g'), 'r', -1,"UTF-8") as prelimfile: # Pull functions into a single string
        prelimcode = prelimfile.read()
    with open((mainpath +'/deltasetstotest.g'), 'r', -1, "UTF-8") as deltafile: # Pull tests into single string
        dtests =  deltafile.read() # file containing all generated commands
    cmd = [GAPpath,prelimcode] # construct object containing path to GAP and the functions
   #P = subprocess.run(cmd, text=True)# spawn a new process, GAP, and pass the the function string to it.
    process = Popen(cmd, text=True, stdin=PIPE, stdout=PIPE)
    process.communicate(dtests)

我希望python将生成的所有命令传递给gap,然后让gap评估这些命令

相反,GAP会启动并定义所需的所有功能,然后挂起。 python程序读取以下任何行后终止:

process.communicate(file path containing commands)
process.communicate(string of all commands)
process.stdin.write(String of a single command)
process.stdin.write(string of all commands)

0 个答案:

没有答案