我到处都读到 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)
由于新用户在发布后八小时内回答他们自己的问题的限制,我无法回答我自己的问题。