2

I have read everywhere that the subprocess module is the best way to use the command line from Python, but I'm having trouble making it work. I have a firefox extension called Page Saver that saves an image of an entire webpage. On the comand line, this command successfully saves an image:

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

I've tried this script to automate the process, but no luck:

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

I get this error:

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

Am I using subprocess incorrectly? Thanks.

Update: Found the solution. It was a little detail of the extension I was using. The script had to be run from the Firefox default saves folder to work. I also ran the parameters as a string, not a list, with shell=True:

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

I could not answer my own question due to the limitation on new users answering their own questions within eight hours of posting.

1 Answer 1

1

What subprocess.call() is telling you with that error is that it cannot find the firefox command on your PATH. (Which is a list of directories where Windows searches for commands).

So you have two options here:

  1. Specify the full path to the Firefox command in your script: this is easy, just put the full path in your python code.
  2. Add the directory that contains the Firefox command to your PATH. This is a little more complicated but you can find a decent tutorial on Super User
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.