2013-11-04 38 views
2

我已經閱讀無處不在,子進程模塊是從Python使用命令行的最佳方式,但是我無法使其工作。我有一個名爲Page Saver的Firefox擴展,它保存了整個網頁的圖像。在COMAND線,此命令成功保存的圖像:在Python中執行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的默認保存文件夾中運行才能正常工作。我也跑了參數字符串,而不是一個列表,殼=真:

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

我不能回答我的問題,由於對新用戶8小時張貼的內回答自己的問題的限制。

回答

1

什麼subprocess.call()告訴你這個錯誤是它無法找到你的PATH上的firefox命令。 (這是Windows搜索命令的目錄列表)。

所以,你這裏有兩種選擇:

  1. 指定的完整路徑,在腳本Firefox的命令:這很容易,只要把完整路徑在Python代碼。
  2. 將包含Firefox命令的目錄添加到您的PATH。這有點複雜,但你可以找到一個體面的教程Super User