Reputation: 800
I want run a command in shell. All of us know we can use the os.system(). But this command must need to be hit enter key to make this command complete.
Now I do it like follow:
from subprocess import Popen, PIPE
Popen(my_cmd.split(), stdin=PIPE).stdin.write('\n')
but it is asynchronous. I want to know, is there possible to use os.system() or some simple way to implement it.
Upvotes: 4
Views: 813
Reputation: 91119
os.system()
is, while not deprecated, seen as superfluous by many people. Everything that can be done with it can be done using subprocess
as well.
If you don't like the fact that your program and the other process run concurrently (I think that's what you mean with asynchronous), you could do
from subprocess import Popen, PIPE
sp = Popen(my_cmd.split(), stdin=PIPE)
sp.stdin.write('\n')
sp.wait()
This last .wait()
call makes the subprocess call "kind of synchronous".
Upvotes: 2