how to use os.system() in python for running an shell order

user2703588 picture user2703588 · Aug 21, 2013 · Viewed 41.5k times · Source

In some shell script, you need to confirm "yes" to run the shell, well, an easier way is using "yes" and pipe, like this:

yes | test.py

then, you can run the shell script automatically without answer "yes" anymore. today, when i use this in python by trying : os.system("yes|**.sh"), i got an fault.

Here is my test.py file:

import os
def f():
    cmd1 = "yes | read "          
    os.system(cmd1)
f()

and run in shell by typing : python test.py. the fault information is : yes: standard output: Broken pipe yes: write error

but if i type "yes|read" in shell,it works well. may anyone tell me why?

Answer

duck picture duck · Aug 21, 2013

try this

import os
def f():
    cmd1 = "echo 'yes' | read "
    os.system(cmd1)
f()