Skip to content Skip to sidebar Skip to footer

Python Subprocess Use Cmd For Print() Commands

I have a python code that should execute another code in some other file. For reasons I don't have the time to explain now I need to use the subprocess-module or something similar.

Solution 1:

The argument shell=True will only execute the command in a shell, in the default shell in your system /bin/sh. To start a new terminal window, you need to specify the terminal:

subprocess.Popen(["xterm", "python"])

The above line opens a new xterm terminal window and executes python command in it.


Solution 2:

on windows you can open a new cmd window and execute the code on that

from subprocess import Popen
from subprocess import Popen, CREATE_NEW_CONSOLE

def startFileInNewProcess(filename):
    terminal='cmd'
    command='Python'
    command=terminal +' '+ '/K' +' '+command+' '+filename
    #/K keeps the command prompt open when execution takes place
    #CREATE_NEW_CONSOLE opens a new console
    proc = subprocess.Popen(command,creationflags=CREATE_NEW_CONSOLE)

Post a Comment for "Python Subprocess Use Cmd For Print() Commands"