cmd执行Python后,不会退出 怎么办

cmd 执行 Python 后,不会退出 怎么办
只有点 X 才能关闭
sys.exit (0) 不能用
sys.exit(0)
os._exit(0)
subprocess.call(‘taskkill /F /IM cmd.exe’, shell=True)
quit()

都不能用

Jason990420
最佳答案

Try this,

import os

# Your python code here

os.system("taskkill /f /im cmd.exe")
4年前 评论
讨论数量: 15
Jason990420

Try this,

import os

# Your python code here

os.system("taskkill /f /im cmd.exe")
4年前 评论

@Jason990420 试一下一下。没反应

file

file

4年前 评论
Jason990420

Script exit after sys.exit(0) and os._exit(0), so os.system not executed !!

4年前 评论

@Jason990420 right! you are brilliant 是的 你真厉害!

4年前 评论

@Jason990420 现在出了新的问题 在 cmd 中手动运行没有问题,放在程序中运行却得不到结果

file

file

4年前 评论
Jason990420

也许您的脚本有问题,如果可能,请提供简短的源代码及运行步骤,显示您如何运行,三言两语容易漏掉问题点.

4年前 评论

@Jason990420 这是我的 script def predict (): test_img1 = cv2.imread ("test_data/test1.jpg")

#生成图像的副本,这样就能保留原始图像 img = test_img1.copy ()

#检测人脸 face, rect = detect_face (img)

#预测人脸 threshold=0.0 label = face_recognizer.predict (face)

获取由人脸识别器返回的相应标签的名称

#if (label[0]==-1):

return None

#print(label)

#cv2.face.face_recognizer.compare_faces(tolerance=0.39) if(label[1]>82): label_text = "no"

在检测到的脸部周围画一个矩形

draw_rectangle(img, rect)

标出预测的名字

draw_text(img, label_text, rect[0], rect[1] - 5)
print(label[1])

print(label[1]) cv2.waitKey(0)

#time.sleep(30) os.system("taskkill /f /im cmd.exe") os.system("taskkill /f /im python.exe")

#subprocess.call('taskkill /F /IM cmd.exe', shell=True)
#quit()
#cv2.waitKey(1)

#os.system("taskkill /f /im Response.exe")

#cv2.destroyAllWindows()

if name == "main":

#加载测试图像

#调用 prepare_training_data()函数 faces, labels = prepare_training_data ("trainingData")

#创建 LBPH 识别器并开始训练,当然也可以选择 Eigen 或者 Fisher 识别器

#print (faces,labels) face_recognizer = cv2.face.LBPHFaceRecognizer_create() face_recognizer.train(faces, np.array(labels))

#test_img2 = cv2.imread("test_data/test2.jpg")

#test_img3 = cv2.imread("test_data/test3.jpg")

#test_img5 = cv2.imread("test_data/test5.jpg")

#test_img6 = cv2.imread("test_data/test6.jpg")

#执行预测 predict ()

#cv2.waitKey(0)

#cv2.destroyAllWindows()

#sys.exit(0)
#os._exit(0)

这是我用 C# 掉用 cmd 执行的代码 Process p = new Process (); //var p = Process.Start (@"cmd.exe", @"/c E:\BC\WorkingProject\Python\Response.exe"); //Thread.Sleep (3000);

代码已被折叠,点此展开
4年前 评论
Jason990420

代码好乱,代码标示 ```python 前至少空一行

4年前 评论

@Jason990420 1,先创建识别器 这步骤没问题 2,调用 predict() 进行人脸识别 3,函数最后会打印出分数 print(label [1]):

label = face_recognizer.predict(face) if(label[1]>82): label_text = "no" draw_rectangle(img, rect)

标出预测的名字

draw_text(img, label_text, rect[0], rect[1] - 5)
print(label[1])

print(label[1]) os.system("taskkill /f /im cmd.exe") os.system("taskkill /f /im python.exe")

4年前 评论

@Jason990420 代码是有点乱 怎么样才能直接传文件给你呢

我现在怀疑之所以,程序调用没有结果。是因为程序太早被杀掉了,所以没结果。但是我 time.sleep (10),有没有用

4年前 评论
Jason990420

过程及代码也许就是这样而已,与人脸识别无关.

cmd

Microsoft Windows [Version 10.0.18363.1082]
(c) 2019 Microsoft Corporation. All rights reserved.

d:\>copy con hellp.py
import os
print('Hello')
os.system("taskkill /f /im cmd.exe")
^Z
        1 file(s) copied.

d:\>hello.py
Hello

cmd 已关闭

4年前 评论

@Jason990420 是这样的 我在 cmd 中执行也没问题 我用 C# 调用就会出问题

4年前 评论
Jason990420

不会 C#, 也许不用在 Response.py 中关闭,而是在 C# 代码中关闭

p.WaitForExit(1000);
// Close process by sending a close message to its main window.
p.CloseMainWindow();
// Free resources associated with process.
p.Close();
4年前 评论

@Jason990420 可能是在 C# 中关闭吧 无所谓的 我把数据写入数据库了 这样没有问题 感谢兄弟的帮助! 嘿嘿

4年前 评论