【问题标题】:subprocess missing output file子进程缺少输出文件
【发布时间】:2017-10-13 19:03:02
【问题描述】:

我对 python 完全陌生,但我正在努力学习。

我想使用 subprocess 命令运行一个模拟程序,我可以在 bash 环境中的终端中调用该程序。语法很简单: 命令输入文件.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-notebook 单元格中运行它。这个 for 循环用于迭代地更改输入文件,从而系统地改变模拟的条件。我的操作系统是 mac osx。

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

如果有任何帮助,我将不胜感激。如果 popen 不是执行此操作的正确 python 函数,那么任何方向也是如此。

【问题讨论】:

  • 再试一次 `p= subprocess.Popen(['command','inputfile.in'],stderr=subprocess.PIPE,stdout= subprocess.PIPE, stdout=subprocess.PIPE)`。
  • 从子进程中给出错误,文件“”,第 2 行 p=subprocess.Popen(['/usr/local/bin/simpson', fileName ],stdout=subprocess.PIPE,stdout=subprocess.PIPE) SyntaxError: 关键字参数重复
  • 对不起我的错误。我有两次stdout=subprocess.PIPE

标签: macos subprocess jupyter-notebook popen


【解决方案1】:

让我们从这样简单的事情中学习:

# 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 ...)

【讨论】:

  • 这有助于调试进程。尽管 p 通信显示模拟正在那里运行。与输入文件中最终过程的格式有关的最终错误。
猜你喜欢
  • 2017-11-30
  • 2011-09-30
  • 2021-08-04
  • 2018-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-17
相关资源
最近更新 更多