2017-10-13 95 views
0

我对Python完全陌生,但我正在努力学习。子流程缺少输出文件

我想使用subprocess命令来运行一个模拟程序,我可以在终端中在bash环境中调用该程序。语法非常简单: 命令inputfile.in 其中命令是tcltk环境中的一个更大的仿真脚本。

好吧我已经读了很多python文献,并决定使用subprocess命令的Popen功能。

所以,从我的理解,我应该能够格式化命令如下:

p= subprocess.Popen(['command','inputfile.in'],stdout= subprocess.PIPE]) 
    print(p.communicate()) 

此命令的输出是两个文件。当我在终端中运行命令时,我在与原始输入文件相同的目录中得到两个文件。

File1.fid File2.spe. 

当我使用Popen时,有两件事让我困惑。 (1)我没有得到任何输出文件写入输入文件的目录。 (2)值p.communicate存在,表示模拟已运行。

输出文件会发生什么情况。有没有一种特定的方式来调用一个命令函数来产生文件?

我在for循环里面的Jupyter笔记本单元中运行它。这个for循环用于迭代地改变输入文件,从而系统地改变模拟条件。我的操作系统是mac osx。

目标是在for循环的每次迭代中模拟或运行命令,然后将输出文件数据存储在较大的字典中。之后,我想在优化过程中将输出文件数据与实验数据进行迭代比较,以最大限度地减少残差。

我将不胜感激任何帮助。如果popen不是正确的python函数,也是任何方向。

+0

用'P = subprocess.Popen([ 'inputfile.in' 命令',],标准错误= subprocess.PIPE,标准输出=子再试一次。 PIPE,stdout = subprocess.PIPE)'。 – swatchai

+0

,从子进程,文件“”,第2行 p = subprocess.Popen(['/ usr/local/bin/simpson',fileName],stdout = subprocess。 PIPE,stdout = subprocess.PIPE) SyntaxError:重复的关键字参数 – witheve

+0

对不起,我的错误。我有'stdout = subprocess.PIPE'两次。 – swatchai

回答

0

让我们从一些容易这样学习:

# This is equivalent with the command line `dir *.* /s /b` on Windows 
import subprocess 
sp = subprocess.Popen(['dir', '*.*', '/s', '/b'], stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True) 
(std_out, std_err) = sp.communicate() # returns (stdout, stderr) 

# print out if error occur 
print('std_err: ', std_err) # expect ('std_err: ', '') 
# print out saved echoing messages 
print('std_out: ', std_out) # expect ('std_out: ', ... can be a long list ...) 
+0

这有助于调试过程。虽然p沟通显示模拟运行在那里。最终的错误与输入文件中最终进程的格式有关。 – witheve