2

I made a python script in which it runs a specified terminal command.

def main(my_list):
    for item in my_list:
        os.system("myCmd -s {0}".format(item))

    item_list = ['itemA', 'itemB', 'itemC']
    main(item_list)

As the terminal command I am trying to use does not accepts a list in the -s argument, which is the reason that I make it into a python script. While running the script, it works but I notice that it will prompts me with the following question: Are you sure you want to continue (y/n)?

This means I will need to punch in the y on my keyboard, 3 times in the above example. But in the event if my list is huge, and should I not be on my desk, is there any way that I can script to make it accept 'y' as my answer, without me to press anything on the keyboard?

2 Answers 2

1

You'd need to use a pipe, that way you can actually write to that pipe an answer, don't forget to close the pipe once you're done with it.

p = os.popen("myCmd -s {0}".format(item), "w")
p.write("y\n")

popen documentation

Sign up to request clarification or add additional context in comments.

1 Comment

Glad I could help @dissidia
1

Use the pexpect module. It's basically expect used in shell scripts, for Python.

1 Comment

I did not get a chance to test this as it appears I need to install it. But I'm sure it works as well!

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.