2

我到处都读到 subprocess 模块是使用 Python 命令行的最佳方式,但我无法让它工作。我有一个名为 Page Saver 的 Firefox 扩展程序,可以保存整个网页的图像。在命令行上,此命令成功保存图像:

firefox -savepng "http://www.google.com"

我试过这个脚本来自动化这个过程,但没有运气:

import subprocess
subprocess.call(['firefox', '-savepng', 'http://www.google.com'], shell=False)

我收到此错误:

Traceback (most recent call last):
  File "C:/Users/computer_4/Desktop/Scripts/crescentsaver.py", line 2, in <module>
    subprocess.call(['firefox', '-savepng', 'http://www.google.com'], shell=False)
  File "C:\Python27\lib\subprocess.py", line 524, in call
    return Popen(*popenargs, **kwargs).wait()
  File "C:\Python27\lib\subprocess.py", line 711, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 948, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

我是否错误地使用了子流程?谢谢。

更新: 找到解决方案。这是我正在使用的扩展的一个小细节。该脚本必须从 Firefox 默认保存文件夹运行才能工作。我还使用 shell=True 将参数作为字符串而不是列表运行:

import subprocess
subprocess.call('firefox -savepng http://www.google.com', shell=True)

由于新用户在发布后八小时内回答他们自己的问题的限制,我无法回答我自己的问题。

4

1 回答 1

1

该错误告诉您subprocess.call()的是它无法在您的PATH. (这是 Windows 搜索命令的目录列表)。

所以你在这里有两个选择:

  1. 在脚本中指定 Firefox 命令的完整路径:这很简单,只需将完整路径放在 python 代码中即可。
  2. 将包含 Firefox 命令的目录添加到您的 PATH。这有点复杂,但你可以在超级用户上找到一个不错的教程
于 2014-02-07T20:37:22.383 回答