Python: Calling CLI (command line) functions from code

This was a tricky one for me. I was trying to use wget to download a file but it wouldn't work properly!

subprocess.call("wget --tries=3 --retry-connrefused http://dl.dropbox.com/u/446679/dog-bites-cat.jpg")

Once I ran it...

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/lib/python2.6/subprocess.py", line 470, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib/python2.6/subprocess.py", line 623, in __init__
    errread, errwrite)
  File "/usr/lib/python2.6/subprocess.py", line 1141, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

What the hell? Everything is right!

Turns out I was missing a certain keyword in the call. Some functions require you to add in "shell = True".

subprocess.call("wget --tries=3 --retry-connrefused http://dl.dropbox.com/u/446679/dog-bites-cat.jpg", shell = True)

Source

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog