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

41,782

try this

import os
def f():
    cmd1 = "echo 'yes' | read "
    os.system(cmd1)
f()
Share:
41,782
Admin
Author by

Admin

Updated on July 23, 2020

Comments

  • Admin
    Admin almost 4 years

    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?