partben partben - 6 months ago 9
Python Question

How to execute two cmd commands in the same instance of cmd using python

Whenever I try to do

os.system("net view /all")


with python, the code gets executed, but it instantly closes, so I can't read the output. I looked around a bit, and found that you can use

ping 127.0.0.1 -n 6 > nul


to do something like

time.sleep()


in python, but if I do

os.system("net view /all")
os.system("ping 127.0.0.1 -n 6 > nul")


They are executed in different instances of cmd, one after the other, so the problem remains. How would I make them be run in the same instance, or how would I be able to put a delay after

net view /all


so that I can read the output? (Only solutions using stock python libs please)

Answer

Just put them together:

os.system("net view /all; ping 127.0.0.1 -n 6 > nul")