Synchronous way to enter return key after running command in python?

I want to run a command in a shell. We all know that we can use os.system (). But for this command, you must press the enter key to complete this command.

Now I do it as follows:

 from subprocess import Popen, PIPE
 Popen(my_cmd.split(), stdin=PIPE).stdin.write('\n')

but it is asynchronous. I want to know if os.system () can be used or some simple way to implement it.

+4
source share
1 answer

os.system()is, although not obsolete, considered by many to be superfluous. Everything that can be done with it can be done with subprocess.

If you do not like that your program and another process are running at the same time (I think you mean asynchronous), you could do

from subprocess import Popen, PIPE
sp = Popen(my_cmd.split(), stdin=PIPE)
sp.stdin.write('\n')
sp.wait()

.wait() "".

+1

Source: https://habr.com/ru/post/1607475/


All Articles