【发布时间】:2015-05-20 06:51:11
【问题描述】:
我有一个如下函数,当我在 CMD 中执行我的 python 代码时它工作正常(SIMT 是一个可执行文件)。但是,当我使用 py2exe 构建可执行文件时,shell 窗口会迅速出现和消失。所以我搜索并发现我可以使用带有creationflags = 0x08000000的subprocess.popen。但它不起作用。
这是我的功能
def Kill(SIMT):
outfile1 = open('Kill.txt', 'w')
outfile1.write('Kill' + '\r\n')
outfile1.write('x')
outfile1.close()
os.system("type Kill.txt | testclient p . " + SIMT)
os.remove('Kill.txt')
我将 os.system 替换为:
subprocess.Popen(["type Kill.txt | testclient p . ", SIMT], creationflags= 0x08000000, shell=True).communicate()
另外,我需要 shell=True 吗?
【问题讨论】:
-
如果你改为运行
Popen(['testclient', 'p', '.', SIMT], stdin=PIPE, universal_newlines=True).communicate('Kill\nx')会发生什么? -
我应该放 Kill.txt 而不是 PIPE 吗?
Popen(['testclient', 'p', '.', SIMT], stdin=Kill.txt, universal_newlines=True),.communicate('Kill\nx')是做什么的?我不应该有creationflags= 0x08000000吗? -
你在我的评论中看到 Kill.txt 了吗?
-
NameError:未定义全局名称“PIPE”。哦,你的意思是 subprocess.PIPE
-
请勿使用
PIPE和/或communicate(),除非您想为子流程提供输入或获取其输出。start是一个内部 shell 命令。它需要外壳。如果os.system('start ...')对您不起作用,请提出一个单独的问题:确保描述您期望发生什么以及会发生什么。包括示例输入/输出和完整的回溯(如果有)。
标签: python subprocess os.system