使用 subprocess.Popen() 执行 xmgrace 批处理

发布于 2024-11-26 03:20:15 字数 637 浏览 3 评论 0原文

我必须从几个带有数据的文件中制作图表。我已经找到了一种运行简单命令的方法

xmgrace -batch batch.bfile -nosafe -hardcopy

,其中batch.bfile是一个文本文件,其中包含grace命令来打印我想要的图形。我已经手动尝试过了,效果非常好。要对多个文件执行此操作,我只需编辑batch.bfile 中的一个参数,并在每次进行更改时运行相同的命令。

我已经编写了一个Python代码,它编辑batch.bfile并使用for循环遍历所有数据文件。在每个循环步骤中,我想直接在命令行中运行上述命令。

经过一番搜索后,我发现了两种解决方案,一种使用 os.system(),另一种使用 subprocess.Popen(),我只能通过编写使 subprocess.Popen() 工作而不会给出任何错误:

subprocess.Popen("xmgrace -batch batch.bfile -nosafe -hardcopy", shell=True)

问题是,这不起作用实际上,它与直接在命令行中运行命令不同。我已经尝试编写batch.b文件的完整目录,但没有任何改变。

我使用的是 Python 2.7 和 Mac OS 10.7

I have to make graphs from several files with data. I already found a way to run a simple command

xmgrace -batch batch.bfile -nosafe -hardcopy

in which batch.bfile is a text file with grace commands to print the graph I want. I already tried it manually and it works perfectly. To do this with several files I just have to edit one parameter inside batch.bfile and run the same command every time I make a change.

I have already written a python code which edits batch.bfile and goes through all the data files with a for cycle. In each cycle step I want to run the mentioned command directly in the command line.

After searching a bit I found two solutions, one with os.system() and another with subprocess.Popen() and I could only make subprocess.Popen() work without giving any errors by writing:

subprocess.Popen("xmgrace -batch batch.bfile -nosafe -hardcopy", shell=True)

Problem is, this doesn't do anything in practice, i.e., it just isn't the same as running the command directly in the command line. I already tried writing the full directory for the batch.bfile but nothing changed.

I am using Python 2.7 and Mac OS 10.7

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

魂归处 2024-12-03 03:20:15

您是否检查过使用 sh 从命令行运行 xmgrace? (即调用 /bin/sh,然后运行 ​​xmgrace...这应该与设置 shell=true 时 Popen 使用的 shell 相同)。

另一个解决方案是创建一个 shell 脚本(创建一个类似 myscript.sh 的文件,然后从终端运行 chmod +x)。在脚本中调用 xmgrace:

#!/bin/bash
xmgrace -batch batch.bfile -nosafe -hardcopy

然后您可以测试 myscript.sh 是否有效,它应该拾取您的配置文件中可能与 python 不同的任何环境变量。如果这有效,您可以从 python 的 subprocess.Popen('myscript.sh') 调用脚本。您可以通过运行以下命令来检查 python 中为子进程设置的环境变量:

import os
os.environ

Have you checked running xmgrace from the command line using sh? (i.e. invoke /bin/sh, then run xmgrace... which should be the same shell that Popen is using when you set shell=true).

Another solution would be to create a shell script (create a file like myscript.sh, and run chmod +x from the terminal). In the script call xmgrace:

#!/bin/bash
xmgrace -batch batch.bfile -nosafe -hardcopy

You could then test that myscript.sh works, which ought to pick up any environment variables that might be in your profile that might differ from python. If this works, you could call the script from python's subprocess.Popen('myscript.sh'). You can check what the environment variables are set in python for subprocess by running:

import os
os.environ
自由如风 2024-12-03 03:20:15

当使用 Popen 时,您可以将应用程序的输出捕获到 stdout 到 stderr 并在应用程序中打印它 - 这样您就可以看到发生了什么:

from subprocess import Popen, PIPE
ps = Popen(reportParameters,bufsize=512, stdout = PIPE, stderr = PIPE)
if ps:
   while 1:
      stdout = ps.stdout.readline()
      stderr = ps.stderr.readline()
      exitcode = ps.poll()
      if (not stdout and not stderr) and (exitcode is not None):
         break
      if stdout:
         stdout = stdout[:-1]
         print stdout
      if stderr:
         stderr = stderr[:-1]
         print stderr

When use use Popen, you can capture the application's output to stdout to stderr and print it within your application - this way you can see what is happening:

from subprocess import Popen, PIPE
ps = Popen(reportParameters,bufsize=512, stdout = PIPE, stderr = PIPE)
if ps:
   while 1:
      stdout = ps.stdout.readline()
      stderr = ps.stderr.readline()
      exitcode = ps.poll()
      if (not stdout and not stderr) and (exitcode is not None):
         break
      if stdout:
         stdout = stdout[:-1]
         print stdout
      if stderr:
         stderr = stderr[:-1]
         print stderr
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文