python子进程使用cmd进行print()命令 [英] python subprocess use cmd for print() commands

查看:96
本文介绍了python子进程使用cmd进行print()命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 python 代码,它应该在其他文件中执行另一个代码.

I have a python code that should execute another code in some other file.

由于我现在没有时间解释的原因,我需要使用 subprocess-module 或类似的东西.我的功能应该使用我的第二个文件中的 print- 命令应该给出其输出的任何窗口.这是我的文件:

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:

maincode.py:

#import subprocess

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

startFileInNewProcess("mysecondfile.py")

mysecondfile.py:

mysecondfile.py:

import os
print os.getcwd()

就我对 SO 的一些文章的理解而言,参数 shell=True 应该使用 mysecondfile.py 的输出创建一个新窗口.这不会发生!任何人都可以解释原因并请提出改进​​建议...

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...

推荐答案

参数 shell=True 只会在 shell 中执行命令,在你系统的默认 shell 中 /bin/sh.要启动一个新的终端窗口,您需要指定终端:

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"])

上一行打开一个新的 xterm 终端窗口并在其中执行 python 命令.

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

这篇关于python子进程使用cmd进行print()命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆