I am exploring the use of Python to perform tasks in CMD, using os.system()
However, I found a few issues that prevents you from executing more than one command through python, along with the problem that as soon as the task is completed, the window closes.
import os os.system('ipconfig', 'netstat') The error that arose from this was that system() takes at most 1 argument (2 given)
import os os.system('ipconfig') os.system('netstat') The problem that arose from this idea was that as soon as ipconfig had completed, I was ejected from the window and netstat began in a new terminal. After netstat had finished, before I could read the data recieved I was ejected from this terminal too.
How can I run two commands that follow one after the other, but in the same window? (I'd be able to scroll up and view the previous command and it's data.) And how can I prevent CMD from ejecting me from the window?
cmd.exe /c, after which CMD exits, as does the console host process (conhost.exe) because no process is attached to it. In principle, though I don't recommend this in general, you could allocate your own console for CMD to inherit. For example:import os, ctypes;ctypes.WinDLL('kernel32').AllocConsole();os.system('ipconfig');os.system('netstat').import subprocess;DETACHED_PROCESS = 8;result = subprocess.run('ipconfig.exe', encoding='mbcs', creationflags=DETACHED_PROCESS, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE);print('Return Code:', result.returncode);print(result.stdout).