0

I have a functionality where I need to run a command inside a python script. From another answer, I figured call from subprocess module is the safest way. But, I am unable to work through it. I am using python 2.7

This is a smaller version of what I am trying :

import subprocess
a = "echo hello"
subprocess.call([a])

It gives me the following error :

 subprocess.call([a])
  File "/usr/lib/python2.7/subprocess.py", line 522, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

I am unable to figure out why!

2 Answers 2

1

You can pass the commands as a string or a list but not as a string in a list, otherwise the system is trying to run the echo hello process (which doesn't exist, obviously, which explains the OSError: [Errno 2] No such file or directory error message). Passing it as a string requires shell=True on some systems.

And shell=True is also required with shell built-ins like the echo command (which has a non built-in version in /bin on some systems, just to add to the confusion)

import subprocess
subprocess.call(["echo","hello"],shell=True)

For non built-in commands (I assume that echo is just a test here), avoid shell=True, since it adds an unnecessary shell layer which degrades startup performance, and is prone to code injection (echo hello; rm -rf everything_on_disk)

To run your favorite editor for instance, you can do:

subprocess.call(["emacs","readme.txt"])
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, just a follow up question : I want to write a string src to a file. So, this is how I should do it? inputFile = "nameInput" c1 = ["echo" ,src, ">>", inputFile] call(c1, shell=True)
Follow up questions aren't really on-topic here, but check this one it answers perfectly: stackoverflow.com/questions/4965159/…
ya, just wanted to be sure if what I understood was right. Thanks :)
with my answer + all the info on the site (google search, don't use site search as it sucks) you'll find everything.
1

The code you have written has issues, subprocess.call takes in a list where the first element of the list is the command. In your case it is echo and hello is your argument it should be the next value in the list. So your code should look like this.

import subprocess

a = [ "echo","hello"]
subprocess.call(a)

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.