【问题标题】:Execute Firefox command in Python在 Python 中执行 Firefox 命令
【发布时间】:2013-11-15 10:32:48
【问题描述】:

我到处都读到 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)

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

【问题讨论】:

    标签: firefox python-2.7 command-line subprocess


    【解决方案1】:

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

    所以你有两个选择:

    1. 在脚本中指定 Firefox 命令的完整路径:这很简单,只需将完整路径放在 python 代码中即可。
    2. 将包含 Firefox 命令的目录添加到您的 PATH。这有点复杂,但你可以在Super User 上找到一个不错的教程

    【讨论】:

      猜你喜欢
      • 2016-03-24
      • 2017-07-04
      • 1970-01-01
      • 2022-08-18
      • 1970-01-01
      • 2017-11-30
      • 1970-01-01
      • 2017-08-04
      • 1970-01-01
      相关资源
      最近更新 更多