【发布时间】: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 问题。
非常感谢任何想法。
【问题讨论】: