Python 使用subprocess.call中文件的输入不起作用,而os.system起作用

Python 使用subprocess.call中文件的输入不起作用,而os.system起作用,python,subprocess,Python,Subprocess,我正在尝试为一个非常古老的FORTRAN程序创建一个python3包装器。基本上,我需要运行该程序,并像这样(从命令行)将输入从文件直接输入到该程序 然后用任何东西创建一个输入文件,这就是我使用的(input.txt) 在命令行中,此操作不会出现问题: python pythonInput.py < input.txt 系统可以工作,但是subprocess.call不能,它只是挂在那里等待。我尝试让它使用shell=True标志,如下所示: import subprocess impo

我正在尝试为一个非常古老的FORTRAN程序创建一个python3包装器。基本上,我需要运行该程序,并像这样(从命令行)将输入从文件直接输入到该程序

然后用任何东西创建一个输入文件,这就是我使用的(input.txt)

在命令行中,此操作不会出现问题:

python pythonInput.py < input.txt
系统可以工作,但是subprocess.call不能,它只是挂在那里等待。我尝试让它使用shell=True标志,如下所示:

import subprocess
import os
os.system('python pythonInput.py < input.txt')
subprocess.call(['python', 'pythonInput.py', '<', 'input.txt'], shell=True)
但这也不行

您知道我如何使用subprocess.call或其他一些方法来实现这一点吗?我可以使用os.system强制执行一个执行时间


谢谢,

您需要编写不带列表的整个命令:

subprocess.call('python pythonInput.py < input.txt', shell=True)
subprocess.call('python-pythonInput.py

这将起作用。

或者,您可以将stdin作为参数传递给
call()

python pythonInput.py < input.txt
[khoopes@computer ~]$ python pythonInput.py < input.txt
Enter some input: RealName
[khoopes@computer ~]$
import subprocess
import os
os.system('python pythonInput.py < input.txt')
subprocess.call(['python', 'pythonInput.py', '<', 'input.txt'])
[khoopes@computer ~]$ python pythonRunTester.py
Enter some input: RealName
Enter some input:
import subprocess
import os
os.system('python pythonInput.py < input.txt')
subprocess.call(['python', 'pythonInput.py', '<', 'input.txt'], shell=True)
[khoopes@computer ~]$ python pythonRunTester.py
Enter some input: RealName
Python 3.6.8 (default, Aug  7 2019, 17:28:10)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
subprocess.call('python pythonInput.py < input.txt', shell=True)
x = subprocess.call(['python', 'pythonInput.py'], stdin=open("/tmp/test.txt"))