{"id":1172363,"date":"2025-01-15T16:48:25","date_gmt":"2025-01-15T08:48:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1172363.html"},"modified":"2025-01-15T16:48:28","modified_gmt":"2025-01-15T08:48:28","slug":"%e5%9c%a8dos%e4%b8%ad%e5%a6%82%e4%bd%95%e9%80%80%e5%87%bapython","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1172363.html","title":{"rendered":"\u5728dos\u4e2d\u5982\u4f55\u9000\u51fapython"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26074453\/5c37a181-01f7-4efd-8e23-b146777cb61e.webp\" alt=\"\u5728dos\u4e2d\u5982\u4f55\u9000\u51fapython\" \/><\/p>\n<p><p> <strong>\u5728DOS\u4e2d\u9000\u51faPython\u7684\u65b9\u6cd5\u6709\u591a\u79cd<\/strong>\uff0c\u5305\u62ec\u4f7f\u7528\u5feb\u6377\u952e\u3001\u5185\u7f6e\u547d\u4ee4\u3001\u6216\u811a\u672c\u65b9\u5f0f\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f<strong>\u6309Ctrl+Z\u5e76\u56de\u8f66\u3001\u4f7f\u7528exit()\u51fd\u6570\u3001\u4f7f\u7528quit()\u51fd\u6570<\/strong>\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528\u5feb\u6377\u952eCtrl+Z\u5e76\u56de\u8f66\u7684\u65b9\u5f0f\uff1a<\/p>\n<\/p>\n<p><p><strong>\u5feb\u6377\u952eCtrl+Z\u5e76\u56de\u8f66<\/strong>\uff1a\u8fd9\u662f\u5728Windows\u547d\u4ee4\u884c\uff08DOS\uff09\u4e2d\u9000\u51faPython\u89e3\u91ca\u5668\u7684\u6700\u76f4\u63a5\u65b9\u6cd5\u3002\u5728\u8fd0\u884cPython\u89e3\u91ca\u5668\u65f6\uff0c\u6309\u4e0bCtrl\u952e\u548cZ\u952e\uff0c\u7136\u540e\u6309\u56de\u8f66\u952e\u5373\u53ef\u7acb\u5373\u9000\u51faPython\u73af\u5883\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\u975e\u5e38\u9ad8\u6548\u548c\u4fbf\u6377\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u5feb\u901f\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5feb\u6377\u952e\u9000\u51fa<\/h3>\n<\/p>\n<p><p>\u5728DOS\u4e2d\u9000\u51faPython\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u4e4b\u4e00\u5c31\u662f\u4f7f\u7528\u5feb\u6377\u952eCtrl+Z\u5e76\u56de\u8f66\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u5b83\u4e0d\u9700\u8981\u8f93\u5165\u4efb\u4f55\u547d\u4ee4\uff0c\u53ea\u9700\u4f7f\u7528\u952e\u76d8\u7684\u5feb\u6377\u952e\u7ec4\u5408\u5373\u53ef\u5b8c\u6210\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Ctrl+Z\u5e76\u56de\u8f66<\/strong>\uff1a\u5728Windows\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u5f53\u4f60\u8fd0\u884cPython\u89e3\u91ca\u5668\u5e76\u5e0c\u671b\u9000\u51fa\u65f6\uff0c\u53ef\u4ee5\u6309\u4e0bCtrl\u952e\u548cZ\u952e\u7ec4\u5408\uff0c\u7136\u540e\u6309\u56de\u8f66\u952e\u3002\u8fd9\u5c06\u7acb\u5373\u9000\u51faPython\u89e3\u91ca\u5668\uff0c\u8fd4\u56de\u5230DOS\u63d0\u793a\u7b26\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>Ctrl+D\uff08\u9002\u7528\u4e8eUnix\u7cfb\u7edf\uff09<\/strong>\uff1a\u5982\u679c\u4f60\u5728Unix\u6216Linux\u7cfb\u7edf\u4e0a\u5de5\u4f5c\uff0c\u53ef\u4ee5\u6309\u4e0bCtrl\u952e\u548cD\u952e\u7ec4\u5408\u6765\u9000\u51faPython\u89e3\u91ca\u5668\u3002\u8fd9\u5728\u529f\u80fd\u4e0a\u4e0eCtrl+Z\u5728Windows\u4e0a\u7684\u4f5c\u7528\u76f8\u540c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5185\u7f6e\u547d\u4ee4\u9000\u51fa<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u5feb\u6377\u952e\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684\u547d\u4ee4\u6765\u9000\u51faPython\u89e3\u91ca\u5668\u3002\u8fd9\u4e9b\u547d\u4ee4\u5728Python\u4ea4\u4e92\u5f0f\u73af\u5883\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>exit()\u51fd\u6570<\/strong>\uff1a\u8fd9\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u9000\u51faPython\u89e3\u91ca\u5668\u3002\u4f60\u53ea\u9700\u8981\u5728\u4ea4\u4e92\u5f0f\u73af\u5883\u4e2d\u8f93\u5165exit()\u5e76\u6309\u56de\u8f66\u952e\uff0c\u5373\u53ef\u9000\u51faPython\u89e3\u91ca\u5668\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>quit()\u51fd\u6570<\/strong>\uff1a\u8fd9\u4e2a\u51fd\u6570\u4e0eexit()\u529f\u80fd\u76f8\u540c\uff0c\u4e5f\u53ef\u4ee5\u7528\u6765\u9000\u51faPython\u89e3\u91ca\u5668\u3002\u8f93\u5165quit()\u5e76\u6309\u56de\u8f66\u952e\u5373\u53ef\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7cfb\u7edf\u547d\u4ee4\u9000\u51fa<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u4f60\u53ef\u80fd\u5e0c\u671b\u5728Python\u811a\u672c\u4e2d\u7f16\u5199\u4ee3\u7801\u6765\u9000\u51fa\u89e3\u91ca\u5668\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u7cfb\u7edf\u547d\u4ee4\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>sys.exit()\u51fd\u6570<\/strong>\uff1aPython\u7684sys\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2aexit()\u51fd\u6570\uff0c\u7528\u4e8e\u5728\u811a\u672c\u4e2d\u9000\u51fa\u89e3\u91ca\u5668\u3002\u9996\u5148\u9700\u8981\u5bfc\u5165sys\u6a21\u5757\uff0c\u7136\u540e\u8c03\u7528sys.exit()\u51fd\u6570\u5373\u53ef\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>sys.exit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>os._exit()\u51fd\u6570<\/strong>\uff1aos\u6a21\u5757\u4e2d\u7684_exit()\u51fd\u6570\u4e5f\u662f\u4e00\u4e2a\u9000\u51faPython\u89e3\u91ca\u5668\u7684\u65b9\u6cd5\uff0c\u901a\u5e38\u7528\u4e8e\u5728\u5b50\u8fdb\u7a0b\u4e2d\u9000\u51fa\u800c\u4e0d\u6e05\u7406\u8d44\u6e90\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>os._exit(0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5f02\u5e38\u5904\u7406\u9000\u51fa<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\u6765\u9000\u51faPython\u89e3\u91ca\u5668\u3002\u8fd9\u79cd\u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u5904\u7406\u5f02\u5e38\u60c5\u51b5\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6355\u83b7KeyboardInterrupt\u5f02\u5e38<\/strong>\uff1a\u5728\u4ea4\u4e92\u5f0f\u73af\u5883\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u6355\u83b7KeyboardInterrupt\u5f02\u5e38\u6765\u9000\u51faPython\u89e3\u91ca\u5668\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    while True:<\/p>\n<p>        pass<\/p>\n<p>except KeyboardInterrupt:<\/p>\n<p>    print(&quot;Exiting...&quot;)<\/p>\n<p>    exit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6355\u83b7SystemExit\u5f02\u5e38<\/strong>\uff1asys.exit()\u51fd\u6570\u4f1a\u5f15\u53d1\u4e00\u4e2aSystemExit\u5f02\u5e38\uff0c\u4f60\u53ef\u4ee5\u6355\u83b7\u8fd9\u4e2a\u5f02\u5e38\u6765\u6267\u884c\u4e00\u4e9b\u6e05\u7406\u5de5\u4f5c\uff0c\u7136\u540e\u9000\u51fa\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>try:<\/p>\n<p>    sys.exit()<\/p>\n<p>except SystemExit:<\/p>\n<p>    print(&quot;Performing cleanup tasks...&quot;)<\/p>\n<p>    r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se  # \u91cd\u65b0\u5f15\u53d1\u5f02\u5e38\u4ee5\u9000\u51fa<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u901a\u8fc7\u811a\u672c\u9000\u51fa<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u811a\u672c\u6765\u9000\u51faPython\u89e3\u91ca\u5668\uff0c\u8fd9\u5728\u81ea\u52a8\u5316\u4efb\u52a1\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5728\u811a\u672c\u4e2d\u8c03\u7528exit()\u6216quit()\u51fd\u6570<\/strong>\uff1a\u4f60\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u76f4\u63a5\u8c03\u7528exit()\u6216quit()\u51fd\u6570\u6765\u9000\u51fa\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">exit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528sys.exit()\u51fd\u6570<\/strong>\uff1a\u5728\u811a\u672c\u4e2d\u8c03\u7528sys.exit()\u51fd\u6570\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u9000\u51fa\u65b9\u6cd5\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>sys.exit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728DOS\u4e2d\u9000\u51faPython\u89e3\u91ca\u5668\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec<strong>\u6309Ctrl+Z\u5e76\u56de\u8f66<\/strong>\u3001<strong>\u4f7f\u7528exit()\u51fd\u6570<\/strong>\u3001<strong>\u4f7f\u7528quit()\u51fd\u6570<\/strong>\u3001<strong>\u8c03\u7528sys.exit()\u51fd\u6570<\/strong>\u3001<strong>\u4f7f\u7528os._exit()\u51fd\u6570<\/strong>\uff0c\u4ee5\u53ca\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\u6765\u9000\u51fa\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u5e94\u7528\u573a\u666f\u548c\u4f18\u52bf\uff0c\u9009\u62e9\u9002\u5408\u4f60\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u548c\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728DOS\u4e2d\u5982\u4f55\u5b89\u5168\u9000\u51faPython\u4ea4\u4e92\u5f0f\u73af\u5883\uff1f<\/strong><br \/>\u8981\u5b89\u5168\u9000\u51faPython\u4ea4\u4e92\u5f0f\u73af\u5883\uff0c\u53ef\u4ee5\u4f7f\u7528<code>exit()<\/code>\u6216<code>quit()<\/code>\u547d\u4ee4\u3002\u8fd9\u4e24\u4e2a\u547d\u4ee4\u90fd\u53ef\u4ee5\u5728Python\u63d0\u793a\u7b26\u4e0b\u76f4\u63a5\u8f93\u5165\uff0c\u4e4b\u540e\u6309\u56de\u8f66\u952e\u5373\u53ef\u9000\u51fa\u3002\u6b64\u5916\uff0c\u60a8\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5feb\u6377\u952e<code>Ctrl + Z<\/code>\uff08Windows\uff09\u6216<code>Ctrl + D<\/code>\uff08Linux\/Mac\uff09\u6765\u5b9e\u73b0\u9000\u51fa\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728DOS\u4e2d\u67e5\u770bPython\u7684\u7248\u672c\u4fe1\u606f\uff1f<\/strong><br \/>\u5728DOS\u4e2d\u67e5\u770bPython\u7248\u672c\u4fe1\u606f\u975e\u5e38\u7b80\u5355\u3002\u60a8\u53ea\u9700\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>python --version<\/code>\u6216<code>python -V<\/code>\uff0c\u7136\u540e\u6309\u56de\u8f66\u952e\u3002\u8fd9\u5c06\u663e\u793a\u60a8\u5f53\u524d\u5b89\u88c5\u7684Python\u7248\u672c\uff0c\u786e\u4fdd\u60a8\u4f7f\u7528\u7684\u662f\u6240\u9700\u7684\u7248\u672c\u3002<\/p>\n<p><strong>\u5728DOS\u4e2d\u5982\u4f55\u8fd0\u884cPython\u811a\u672c\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u5728DOS\u4e2d\u8fd0\u884cPython\u811a\u672c\u6587\u4ef6\uff0c\u60a8\u9700\u8981\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\u5e76\u5bfc\u822a\u5230\u811a\u672c\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\u3002\u63a5\u7740\uff0c\u8f93\u5165<code>python script_name.py<\/code>\uff0c\u5c06<code>script_name.py<\/code>\u66ff\u6362\u4e3a\u60a8\u7684\u811a\u672c\u6587\u4ef6\u540d\u3002\u6309\u4e0b\u56de\u8f66\u952e\u540e\uff0cPython\u4f1a\u6267\u884c\u8be5\u811a\u672c\u5e76\u5728\u547d\u4ee4\u884c\u4e2d\u663e\u793a\u8f93\u51fa\u7ed3\u679c\u3002\u786e\u4fddPython\u5df2\u6b63\u786e\u5b89\u88c5\u5e76\u5728\u7cfb\u7edf\u8def\u5f84\u4e2d\u53ef\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728DOS\u4e2d\u9000\u51faPython\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5feb\u6377\u952e\u3001\u5185\u7f6e\u547d\u4ee4\u3001\u6216\u811a\u672c\u65b9\u5f0f\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u6309Ctrl+Z\u5e76\u56de [&hellip;]","protected":false},"author":3,"featured_media":1172373,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172363"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1172363"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172363\/revisions"}],"predecessor-version":[{"id":1172376,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172363\/revisions\/1172376"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1172373"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1172363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1172363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1172363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}