0

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. My fuction should use any window in which the print- commands in my second file should give their output. Here are my files:

maincode.py:

#import subprocess

def startFileInNewProcess(filename):
    proc = subprocess.Popen(["python", filename], shell=True)

startFileInNewProcess("mysecondfile.py")

mysecondfile.py:

import os
print os.getcwd()

As far as I undestood some articles on SO, the parameter shell=True should create a new window with the output of the mysecondfile.py. This does not happen! Can anybody explain why and please give improvement proposals...

4
  • I'm on a debian system, more detailed: Raspbian Commented Nov 24, 2017 at 20:13
  • No, shell=True doesn't create a new window. It just means that the specified command will be executed through the shell. See docs.python.org/3/library/… Commented Nov 24, 2017 at 20:16
  • 2
    No, shell=True causes the first argument to be converted to a string and used as an argument to sh -c. You don't want shell=True if you are passing a list. Commented Nov 24, 2017 at 20:16
  • Here are some other helpful links, related questions link, link Commented Nov 24, 2017 at 20:44

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I applie your proposal to my code and got this presuming that filename is executable: subprocess.Popen(["xterm", "./"+filename]) This does not wait until I pressed enter or something similar, but codes the window immediately afer end of the program...
0

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)

1 Comment

As I mentioned above, I'm on linux, but thanks for the proposal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.