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")
3年前 评论
讨论数量: 15
Jason990420

Try this,

import os

# Your python code here

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

@Jason990420 试一下一下。没反应

file

file

3年前 评论
Jason990420

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

3年前 评论

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

3年前 评论

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

file

file

3年前 评论
Jason990420

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

3年前 评论

@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);

        //string path = "E:\\BC\\WorkingProject\\Python\\Response.py";//待处理python文件的路径,本例中放在debug文件夹下
        //string sArguments = @"cd / c / d \" + @"E:\BC\WorkingProject\Python" + "\""+@"/c E:\BC\WorkingProject\Python\Response.exe"";
        try
        {

            //ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(path);
            p.StartInfo.FileName = @"cmd.exe";
            //p.StartInfo.WorkingDirectory = Application.StartupPath;
            //p.StartInfo.Arguments = sArguments;
            //p.StartInfo.FileName = @"E:\BC\WorkingProject\Python\Response.exe"; //python2.7的安装路径
            //p.StartInfo.Arguments = "E:\\BC\\WorkingProject\\Python\\Response.py";
            //sArguments += " " + "";
            //p.StartInfo.Arguments = sArguments;//python命令的参数


           ;
            //p.StartInfo.WorkingDirectory = @"E:\BC\WorkingProject\Python";
            //System.AppDomain.CurrentDomain.BaseDirectory;

            p.StartInfo.UseShellExecute = false;    //是否使用操作系统shell启动
           // p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
           // p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
            p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
            p.StartInfo.CreateNoWindow = true;//不显示程序窗口
            p.StartInfo.RedirectStandardInput = true;  //重定向命令行的输入bai流,以便我们可以通过程序输入命令以模仿手工输入
            p.StartInfo.RedirectStandardOutput = true;  //输出流重定向,保证可以获取命令行执行命令后的输出结果
            //p.StandardInput.WriteLine("cd /c /d \"" + @"E:\BC\WorkingProject\Python" + "\"");

            p.Start();
            //p.StandardInput.WriteLine("help"); //输入各种控制命令
            //p.StandardInput.WriteLine("E:"); //输入各种控制命令
            //p.StandardInput.AutoFlush = true;
            p.StandardInput.WriteLine(@"cd E:\BC\WorkingProject\Python");
            p.StandardInput.WriteLine(@"Response.py");

            //while (!p.HasExited)

            //p.StandardInput.WriteLine(@"os.system('taskkill / f / im cmd.exe')");
            //p.StandardInput.WriteLine(@"os.system('taskkill / f / im python.exe')");

            //{

            //p.StandardInput.WriteLine(@"exit");

            //}
            //p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);

            //int returnValue = p.ExitCode;


            //p.WaitForExit();//等待程序执行完推出进程



            //Process[] MyProcess = Process.GetProcessesByName("cmd.exe");

            //MyProcess[0].Kill();
            //MyProcess = Process.GetProcessesByName("python.exe");

            //MyProcess[0].Kill();
            // p.WaitForExit();

            MessageBox.Show(p.StandardOutput.ReadToEnd());
            //StreamReader reader = p.StandardOutput;//截取输出流
            ////string line = reader.ReadLine();//每次读取一行
            //foreach (var line in reader.ReadLine())
            //{
            //    if (int.Parse(line.ToString()) <= 80)
            //    {
            //        MessageBox.Show("人脸识别成功!");
            //    }
            //}
            p.Close();
3年前 评论
Jason990420

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

3年前 评论

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

3年前 评论

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

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

3年前 评论
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 已关闭

3年前 评论

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

3年前 评论
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();
3年前 评论

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

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!