1. <tfoot id='n8Zre'></tfoot>

      1. <i id='n8Zre'><tr id='n8Zre'><dt id='n8Zre'><q id='n8Zre'><span id='n8Zre'><b id='n8Zre'><form id='n8Zre'><ins id='n8Zre'></ins><ul id='n8Zre'></ul><sub id='n8Zre'></sub></form><legend id='n8Zre'></legend><bdo id='n8Zre'><pre id='n8Zre'><center id='n8Zre'></center></pre></bdo></b><th id='n8Zre'></th></span></q></dt></tr></i><div id='n8Zre'><tfoot id='n8Zre'></tfoot><dl id='n8Zre'><fieldset id='n8Zre'></fieldset></dl></div>

        <legend id='n8Zre'><style id='n8Zre'><dir id='n8Zre'><q id='n8Zre'></q></dir></style></legend>
          <bdo id='n8Zre'></bdo><ul id='n8Zre'></ul>

        <small id='n8Zre'></small><noframes id='n8Zre'>

        为什么有时 Python 子进程在运行进程后无法获得正确的退出代码?

        时间:2023-09-03

        <small id='P4BLY'></small><noframes id='P4BLY'>

        1. <legend id='P4BLY'><style id='P4BLY'><dir id='P4BLY'><q id='P4BLY'></q></dir></style></legend>
            <bdo id='P4BLY'></bdo><ul id='P4BLY'></ul>

            1. <tfoot id='P4BLY'></tfoot>
              <i id='P4BLY'><tr id='P4BLY'><dt id='P4BLY'><q id='P4BLY'><span id='P4BLY'><b id='P4BLY'><form id='P4BLY'><ins id='P4BLY'></ins><ul id='P4BLY'></ul><sub id='P4BLY'></sub></form><legend id='P4BLY'></legend><bdo id='P4BLY'><pre id='P4BLY'><center id='P4BLY'></center></pre></bdo></b><th id='P4BLY'></th></span></q></dt></tr></i><div id='P4BLY'><tfoot id='P4BLY'></tfoot><dl id='P4BLY'><fieldset id='P4BLY'></fieldset></dl></div>
                  <tbody id='P4BLY'></tbody>

                  本文介绍了为什么有时 Python 子进程在运行进程后无法获得正确的退出代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 Python 子进程在 Windows 7 上运行外部脚本.我正在尝试获取退出代码.

                  I am using Python subprocess to run external scripts on Windows 7. I am trying to get the exit code.

                  在案例 1 中,我运行一个 python 脚本 test1.py.

                  In case 1, I run a python script test1.py.

                  test1.py

                  import sys
                  sys.exit(24)   <--exit code
                  

                  myscript1.py

                  myscript1.py

                  import subprocess
                  process = subprocess.Popen(["python", "C:\path\to\test1.py"], stdout=subprocess.PIPE)
                  process.wait()
                  print process.returncode
                  

                  在 Windows 命令提示符下,当我运行脚本时,我得到以下输出:

                  In Windows command prompt, when I run the script, I get the following output:

                  >python test1.py
                  >
                  >echo %errorlevel%
                  >24
                  >
                  >python myscript1.py
                  >24
                  

                  因此,您可以看到在这种情况下子进程能够获得正确的退出代码.

                  So, you can see that subprocess is able to get the correct exit code in this case.

                  在案例 2 中,我运行一个批处理文件 test2.cmd.

                  In case 2, I run a batch file test2.cmd.

                  test2.cmd

                  EXIT /B 56   <--exit code
                  

                  myscript2.py

                  myscript2.py

                  import subprocess
                  process = subprocess.Popen(["C:\path\to\test2.cmd"], stdout=subprocess.PIPE)
                  process.wait()
                  print process.returncode
                  

                  在 Windows 命令提示符下,当我运行脚本时,我得到以下输出:

                  In Windows command prompt, when I run the script, I get the following output:

                  >test2.cmd
                  >
                  >echo %errorlevel%
                  >56
                  >
                  >python myscript2.py
                  >56
                  

                  因此,您可以看到子进程在这种情况下也能够获得正确的退出代码.

                  So, you can see that subprocess is also able to get the correct exit code in this case.

                  在案例 3 中,我运行 SikuliX 脚本.

                  In case 3, I run a SikuliX script.

                  test3.sikuli

                  test3.sikuli

                  xxx xxx (sikuli script here)
                  xxx xxx
                  ...
                  exit(16)   <--exit code
                  

                  myscript3.py

                  myscript3.py

                  import subprocess
                  process = subprocess.Popen(["C:\path\to\runsikuli.cmd", "-r", "C:\path\to\sikuli-script.sikuli"], stdout=subprocess.PIPE)
                  process.wait()
                  print process.returncode
                  

                  在 Windows 命令提示符下,当我运行脚本时,我得到以下输出:

                  In Windows command prompt, when I run the script, I get the following output:

                  >C:path	o
                  unsikuli.cmd -r C:path	osikuli-script.sikuli
                  >... (stdout + stderr)
                  >16
                  >
                  >echo %errorlevel%
                  >16
                  >
                  >python myscript3.py
                  >0
                  

                  在情况 3 中,当我在命令提示符下手动运行脚本时,它能够设置 %errorlevel%.当我使用 Python 子进程运行脚本时,子进程无法获取正确的退出代码.它总是返回 0.

                  In case 3, when I run the script manually in the command prompt, it is able to set the %errorlevel%. When I run the script using Python subprocess, subprocess is unable to get the correct exit code. It always return 0.

                  案例3为什么Python子进程获取不到退出码?

                  Why Python subprocess failed to get the exit code in case 3?

                  推荐答案

                  As 您的评论说如果您运行相同的命令,那么命令提示符和使用 Python subprocess 模块产生相同的结果(0 退出代码).

                  As your comment says if you run the same command then both the command prompt and using Python subprocess module produce the same result (0 exit code).

                  您可能会看到不同的退出代码,因为您使用了不同的命令.这里没有惊喜.Python subprocess 模块返回正确的(对于给定命令)退出代码.

                  You may see different exit codes because you use different commands. No surprise here. Python subprocess module returns correct (for a given command) exit code.

                  如果它没有返回正确的退出状态,那么你可以添加 &exit 在您的 cmd.exe 命令末尾作为一种解决方法以获得正确的返回码,请参阅 "Windows 上的子进程:Python 问题跟踪器上 shell=True" 的错误返回代码:

                  If it doesn't return the right exit status then you could add & exit at the end of your cmd.exe command as a workaround to get the correct return code, see "subprocess on Windows: wrong return code with shell=True" on Python issue tracker:

                  from subprocess import check_call
                  
                  check_call(r'C:path	o
                  unsikuli.cmd -r C:path	osikuli-script.sikuli & exit',
                             shell=True)
                  

                  这篇关于为什么有时 Python 子进程在运行进程后无法获得正确的退出代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python中无限期的守护进程生成 下一篇:模拟 subprocess.Popen 依赖于导入样式

                  相关文章

                  1. <tfoot id='DGJPg'></tfoot>

                      <small id='DGJPg'></small><noframes id='DGJPg'>

                      <legend id='DGJPg'><style id='DGJPg'><dir id='DGJPg'><q id='DGJPg'></q></dir></style></legend>
                    1. <i id='DGJPg'><tr id='DGJPg'><dt id='DGJPg'><q id='DGJPg'><span id='DGJPg'><b id='DGJPg'><form id='DGJPg'><ins id='DGJPg'></ins><ul id='DGJPg'></ul><sub id='DGJPg'></sub></form><legend id='DGJPg'></legend><bdo id='DGJPg'><pre id='DGJPg'><center id='DGJPg'></center></pre></bdo></b><th id='DGJPg'></th></span></q></dt></tr></i><div id='DGJPg'><tfoot id='DGJPg'></tfoot><dl id='DGJPg'><fieldset id='DGJPg'></fieldset></dl></div>

                        <bdo id='DGJPg'></bdo><ul id='DGJPg'></ul>