简体   繁体   English

CTRL-C在python中终止

[英]CTRL-C terminate in python

Currently, I have an exe that I need to pass commands to thru python. 目前,我有一个exe文件,需要通过python传递命令。 The code worked when it was by itself. 该代码本身就可以工作。 When I merged that snippet with my final program, it failed to work. 当我将该代码段与最终程序合并后,它无法正常工作。

Basically, after the first two files upload using the program shell_start (upload is -f in the program i'm passing commands to), python decides to skip the final upload at the bottom with the program called shell_forward . 基本上,在使用程序shell_start上传前两个文件后(在我向其传递命令的程序中,上传是-f ),python决定使用名为shell_forward的程序跳过底部的最终上传。 The final upload of 3 files with the program shell_forward , doesn't even work. 使用程序shell_forward最终上传3个文件甚至无效。

So, my main question, if you're confused, is: why is it that when the program shell_start finishes uploading the two commands and files, the python shell won't allow me to type anything in it? 因此,如果您感到困惑,我的主要问题是:为什么为什么当shell_start程序完成两个命令和文件的上传后,python shell却不允许我在其中输入任何内容? It acts like a command prompt window that won't let you type anything into it after code is executed. 它就像一个命令提示符窗口,在执行代码后您将无法在其中键入任何内容。

That is why I feel that a ctrl-c is needed to terminate shell_start.exe from the previous process, so python might let me type after it's execution. 这就是为什么我觉得需要一个ctrl-c来终止上一个进程的shell_start.exe的原因,因此python可能会让我在执行后键入内容。

Here's the code: 这是代码:

import os, time

name = raw_input("Input your name: ")
apn = raw_input("Input apn name: ")
ecid = raw_input("Input ecid name: ")
kernel = raw_input("Input kernel name: ")

os.system('shell_start.exe -f %s'%name)
time.sleep(1)
os.system('shell_start.exe -f %s'%apn)
time.sleep(1)

os.system('shell_forward.exe --imagefile myfile.img --ecid %(x)s --kernel %(y)s '% {"x" : ecid, "y" : kernel})

You may have better results by replacing the os.system calls with subprocess . 您可以通过更换具有更好的效果os.system与调用subprocess

The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. 子流程模块允许您生成新流程,连接到其输入/输出/错误管道,并获取其返回代码。 This module intends to replace several other, older modules and functions, such as: 该模块旨在替换其他一些较旧的模块和功能,例如:

 os.system os.spawn* os.popen* popen2.* commands.* 

See also PEP 324 – PEP proposing the subprocess module 另请参阅PEP 324 –提出子流程模块的PEP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM