Pawandeep Singh
Pawandeep Singh

Reputation: 890

Python run multiple prompt commands terminal

I want to run terminal command from python script. I know I can use os.system() call. But problem here is when I run first command I get a prompt in which I have to write next terminal command. For example:-

./distance vectors_bow.bin 
Enter word or sentence (EXIT to break): EXIT

I tried to use os.system('./distance vectors_bow.bin & EXIT') but I get output sh: 1: EXIT: not found.

It works fine when I do the above process manually in terminal but not from python script. How to do it?

Upvotes: 0

Views: 470

Answers (1)

Eran
Eran

Reputation: 2444

If I understand correctly you want to run distance with parameter vectors_bow.bin and have the first input EXIT

try this:

from subprocess import Popen, PIPE
Popen(['distance', 'vectors_bow.bin'], stdin=PIPE).communicate('EXIT'.encode())

EDIT: Fixed for python3 needed encode for the input parameter

Upvotes: 2

Related Questions