【问题标题】:Shell command works in shell, but not when when fired from Python subprocessShell 命令在 shell 中有效,但在从 Python 子进程触发时无效
【发布时间】:2016-07-27 08:49:06
【问题描述】:

我正在尝试从 Python 脚本执行 shell 命令。 我已经尝试过通常的嫌疑人,subprocess.call、Popen、os.system 等。

无可否认,我尝试执行的命令相当长(7k 个字符),因为其中一个参数是 json 字符串。从我读过的内容来看,这里的问题应该不是长度。

命令如下所示:

phantomjs /some/path/visualizer_interface.js -path /another/path/chart.svg -type chart_pie -id 0 -language de -data '{...}'

可视化界面是我自己编写的脚本,它基本上在 Phantom JS 上下文中呈现请求的图表,抓取 svg 并将其写入指定路径。当我执行完全相同的命令时,我得到了一个完美的图表,但在 Python 中,子进程永远不会返回,而且我没有得到任何形式的反馈,甚至在子进程 stdout 上也没有。

with open('/home/max/stdout.txt', 'w') as out:
    res = subprocess.Popen(command, shell=True, stdout=out)
res.wait()

我能够执行其他 shell 命令,所以这不是基本的 Python 问题。

非常感谢任何想法。

【问题讨论】:

    标签: python shell phantomjs


    【解决方案1】:

    原来我在 phantomjs 脚本中有一个非常轻微的错误,它的行为因执行位置而异。

    【讨论】:

      猜你喜欢
      • 2019-07-30
      • 2023-03-24
      • 1970-01-01
      • 2018-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多