{"id":1004116,"date":"2024-12-27T10:23:23","date_gmt":"2024-12-27T02:23:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1004116.html"},"modified":"2024-12-27T10:23:29","modified_gmt":"2024-12-27T02:23:29","slug":"python-%e5%a6%82%e4%bd%95%e4%bc%a0%e5%85%a5%e8%be%93%e5%85%a5%e5%8f%82%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1004116.html","title":{"rendered":"python \u5982\u4f55\u4f20\u5165\u8f93\u5165\u53c2\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081213\/e8e7ed96-0159-45c7-949d-fa623d1ce44b.webp\" alt=\"python \u5982\u4f55\u4f20\u5165\u8f93\u5165\u53c2\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u4f20\u5165\u8f93\u5165\u53c2\u6570\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u3001\u51fd\u6570\u53c2\u6570\u3001\u73af\u5883\u53d8\u91cf\u7b49\u65b9\u5f0f\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u548c\u51fd\u6570\u53c2\u6570\u3002\u547d\u4ee4\u884c\u53c2\u6570\u4f7f\u7528<code>sys.argv<\/code>\u6216<code>argparse<\/code>\u6a21\u5757\u6765\u83b7\u53d6\uff0c\u51fd\u6570\u53c2\u6570\u5219\u662f\u5728\u5b9a\u4e49\u51fd\u6570\u65f6\u901a\u8fc7\u53c2\u6570\u5217\u8868\u4f20\u5165\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e2e\u52a9\u4f60\u66f4\u6df1\u5165\u5730\u7406\u89e3\u5982\u4f55\u5728Python\u4e2d\u4f20\u5165\u8f93\u5165\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u4f20\u5165<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u63a5\u6536\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u6700\u5e38\u7528\u7684\u662f\u901a\u8fc7<code>sys.argv<\/code>\u548c<code>argparse<\/code>\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528sys.argv<\/h3>\n<\/p>\n<p><p><code>sys.argv<\/code>\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542b\u4e86\u547d\u4ee4\u884c\u8f93\u5165\u7684\u53c2\u6570\u3002\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u811a\u672c\u540d\u79f0\uff0c\u540e\u7eed\u7684\u5143\u7d20\u662f\u4f20\u5165\u7684\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    print(&quot;Script name:&quot;, sys.argv[0])<\/p>\n<p>    for i, arg in enumerate(sys.argv[1:], 1):<\/p>\n<p>        print(f&quot;Argument {i}:&quot;, arg)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u6267\u884c\uff0c\u5982\uff1a<code>python script.py arg1 arg2 arg3<\/code>\uff0c\u8f93\u51fa\u5c06\u663e\u793a\u4f20\u5165\u7684\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528argparse\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>argparse<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u547d\u4ee4\u884c\u53c2\u6570\u89e3\u6790\u529f\u80fd\uff0c\u53ef\u4ee5\u5b9a\u4e49\u53c2\u6570\u7c7b\u578b\u3001\u9ed8\u8ba4\u503c\u3001\u5e2e\u52a9\u4fe1\u606f\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import argparse<\/p>\n<p>def main():<\/p>\n<p>    parser = argparse.ArgumentParser(description=&quot;Process some integers.&quot;)<\/p>\n<p>    parser.add_argument(&#39;integers&#39;, metavar=&#39;N&#39;, type=int, nargs=&#39;+&#39;,<\/p>\n<p>                        help=&#39;an integer for the accumulator&#39;)<\/p>\n<p>    parser.add_argument(&#39;--sum&#39;, dest=&#39;accumulate&#39;, action=&#39;store_const&#39;,<\/p>\n<p>                        const=sum, default=max,<\/p>\n<p>                        help=&#39;sum the integers (default: find the max)&#39;)<\/p>\n<p>    args = parser.parse_args()<\/p>\n<p>    print(args.accumulate(args.integers))<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u811a\u672c\u65f6\u53ef\u4ee5\u4f7f\u7528<code>--sum<\/code>\u9009\u9879\u6307\u5b9a\u6c42\u548c\u64cd\u4f5c\uff1a<code>python script.py 1 2 3 --sum<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u901a\u8fc7\u51fd\u6570\u53c2\u6570\u4f20\u5165<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u51fd\u6570\u662f\u7b2c\u4e00\u7c7b\u5bf9\u8c61\uff0c\u53ef\u4ee5\u63a5\u6536\u4efb\u610f\u6570\u91cf\u548c\u7c7b\u578b\u7684\u53c2\u6570\u3002\u51fd\u6570\u53c2\u6570\u53ef\u4ee5\u5206\u4e3a\u4f4d\u7f6e\u53c2\u6570\u3001\u5173\u952e\u5b57\u53c2\u6570\u3001\u9ed8\u8ba4\u53c2\u6570\u548c\u53ef\u53d8\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u4f4d\u7f6e\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u4f4d\u7f6e\u53c2\u6570\u662f\u51fd\u6570\u5b9a\u4e49\u65f6\u6309\u987a\u5e8f\u4f20\u5165\u7684\u53c2\u6570\uff0c\u5728\u8c03\u7528\u65f6\u5fc5\u987b\u63d0\u4f9b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name, age):<\/p>\n<p>    print(f&quot;Hello, my name is {name} and I am {age} years old.&quot;)<\/p>\n<p>greet(&quot;Alice&quot;, 30)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u5173\u952e\u5b57\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u5173\u952e\u5b57\u53c2\u6570\u5728\u8c03\u7528\u65f6\u901a\u8fc7\u53c2\u6570\u540d\u6307\u5b9a\uff0c\u53ef\u4ee5\u4e0d\u6309\u987a\u5e8f\u4f20\u9012\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name, age):<\/p>\n<p>    print(f&quot;Hello, my name is {name} and I am {age} years old.&quot;)<\/p>\n<p>greet(age=30, name=&quot;Bob&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u9ed8\u8ba4\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u9ed8\u8ba4\u53c2\u6570\u662f\u5728\u51fd\u6570\u5b9a\u4e49\u65f6\u6307\u5b9a\u9ed8\u8ba4\u503c\u7684\u53c2\u6570\uff0c\u5982\u679c\u8c03\u7528\u65f6\u672a\u63d0\u4f9b\uff0c\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name, age=25):<\/p>\n<p>    print(f&quot;Hello, my name is {name} and I am {age} years old.&quot;)<\/p>\n<p>greet(&quot;Charlie&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u53ef\u53d8\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u53ef\u53d8\u53c2\u6570\u5141\u8bb8\u51fd\u6570\u63a5\u6536\u4efb\u610f\u6570\u91cf\u7684\u4f4d\u7f6e\u53c2\u6570\u6216\u5173\u952e\u5b57\u53c2\u6570\uff0c\u5206\u522b\u4f7f\u7528<code>*args<\/code>\u548c<code>kwargs<\/code>\u8868\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(*names, info):<\/p>\n<p>    for name in names:<\/p>\n<p>        print(f&quot;Hello, {name}!&quot;)<\/p>\n<p>    for key, value in info.items():<\/p>\n<p>        print(f&quot;{key}: {value}&quot;)<\/p>\n<p>greet(&quot;Dave&quot;, &quot;Eve&quot;, age=40, city=&quot;New York&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u4f20\u5165<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u4f20\u5165\u53c2\u6570\u4e5f\u662f\u4e00\u79cd\u9009\u62e9\uff0c\u53ef\u4ee5\u901a\u8fc7<code>os.environ<\/code>\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def main():<\/p>\n<p>    user = os.environ.get(&#39;USER&#39;, &#39;default_user&#39;)<\/p>\n<p>    path = os.environ.get(&#39;PATH&#39;, &#39;&#39;)<\/p>\n<p>    print(f&quot;User: {user}&quot;)<\/p>\n<p>    print(f&quot;Path: {path}&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u5728\u8fd0\u884c\u811a\u672c\u524d\u901a\u8fc7\u547d\u4ee4\u884c\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff1a<code>export USER=John<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u4f20\u5165\u8f93\u5165\u53c2\u6570\u662fPython\u7f16\u7a0b\u4e2d\u7684\u57fa\u672c\u64cd\u4f5c\uff0c\u7406\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u4f60\u7684\u7a0b\u5e8f\u66f4\u5177\u7075\u6d3b\u6027\u548c\u53ef\u914d\u7f6e\u6027\u3002\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u53ef\u4ee5\u65b9\u4fbf\u5730\u4e0e\u7528\u6237\u4ea4\u4e92\uff0c\u51fd\u6570\u53c2\u6570\u5219\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u590d\u7528\u6027\u548c\u53ef\u8bfb\u6027\uff0c\u800c\u73af\u5883\u53d8\u91cf\u5219\u63d0\u4f9b\u4e86\u4e00\u79cd\u9690\u85cf\u914d\u7f6e\u7684\u65b9\u5f0f\u3002\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u4f60\u7684Python\u7a0b\u5e8f\u66f4\u52a0\u9ad8\u6548\u548c\u7b80\u6d01\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u63a5\u6536\u547d\u4ee4\u884c\u8f93\u5165\u53c2\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>sys<\/code>\u6a21\u5757\u4e2d\u7684<code>argv<\/code>\u6765\u63a5\u6536\u547d\u4ee4\u884c\u4f20\u5165\u7684\u8f93\u5165\u53c2\u6570\u3002<code>argv<\/code>\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542b\u4e86\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u7684\u6240\u6709\u53c2\u6570\uff0c\u5176\u4e2d\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u811a\u672c\u540d\u79f0\uff0c\u540e\u7eed\u7684\u5143\u7d20\u5219\u662f\u4f20\u5165\u7684\u53c2\u6570\u3002\u4f8b\u5982\uff0c<code>python script.py arg1 arg2<\/code>\uff0c\u5728\u811a\u672c\u4e2d\u53ef\u4ee5\u901a\u8fc7<code>sys.argv[1]<\/code>\u548c<code>sys.argv[2]<\/code>\u6765\u8bbf\u95ee<code>arg1<\/code>\u548c<code>arg2<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u54ea\u4e9b\u65b9\u5f0f\u4f20\u9012\u8f93\u5165\u53c2\u6570\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>sys.argv<\/code>\uff0cPython\u8fd8\u652f\u6301\u4f7f\u7528<code>argparse<\/code>\u6a21\u5757\u6765\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u66f4\u4e3a\u7075\u6d3b\uff0c\u5141\u8bb8\u4f60\u8bbe\u7f6e\u53c2\u6570\u7c7b\u578b\u3001\u9ed8\u8ba4\u503c\u4ee5\u53ca\u5e2e\u52a9\u4fe1\u606f\u7b49\u3002\u4f7f\u7528<code>argparse<\/code>\u65f6\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u89e3\u6790\u5668\u5e76\u6dfb\u52a0\u6240\u9700\u7684\u53c2\u6570\uff0c\u968f\u540e\u8c03\u7528<code>parse_args()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u8fd9\u4e9b\u53c2\u6570\u7684\u503c\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u60f3\u5728Python\u811a\u672c\u4e2d\u4f7f\u7528\u4ea4\u4e92\u5f0f\u8f93\u5165\uff0c\u8be5\u600e\u4e48\u505a\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5728\u8fd0\u884c\u65f6\u4e0e\u7528\u6237\u8fdb\u884c\u4ea4\u4e92\u5f0f\u8f93\u5165\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>input()<\/code>\u51fd\u6570\u3002\u8be5\u51fd\u6570\u4f1a\u6682\u505c\u7a0b\u5e8f\u6267\u884c\uff0c\u7b49\u5f85\u7528\u6237\u8f93\u5165\u5e76\u8fd4\u56de\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u5b9e\u65f6\u83b7\u53d6\u7528\u6237\u7684\u8f93\u5165\u6570\u636e\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>name = input(&quot;\u8bf7\u8f93\u5165\u60a8\u7684\u540d\u5b57\uff1a&quot;)<\/code>\u53ef\u4ee5\u8ba9\u7528\u6237\u8f93\u5165\u4ed6\u4eec\u7684\u540d\u5b57\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u53d8\u91cf<code>name<\/code>\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4f20\u5165\u8f93\u5165\u53c2\u6570\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u3001\u51fd\u6570\u53c2\u6570\u3001\u73af\u5883\u53d8\u91cf\u7b49\u65b9\u5f0f\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u548c [&hellip;]","protected":false},"author":3,"featured_media":1004131,"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\/1004116"}],"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=1004116"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1004116\/revisions"}],"predecessor-version":[{"id":1004135,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1004116\/revisions\/1004135"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1004131"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1004116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1004116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1004116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}