{"id":970914,"date":"2024-12-27T05:30:08","date_gmt":"2024-12-26T21:30:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/970914.html"},"modified":"2024-12-27T05:30:10","modified_gmt":"2024-12-26T21:30:10","slug":"linux-python%e5%a6%82%e4%bd%95%e4%bc%a0%e5%8f%82%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/970914.html","title":{"rendered":"linux python\u5982\u4f55\u4f20\u53c2\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24194019\/ccedaa0b-9067-4439-9545-70b7ff3b408a.webp\" alt=\"linux python\u5982\u4f55\u4f20\u53c2\u6570\" \/><\/p>\n<p><p> <strong>\u5728Linux\u4e2d\u4f7f\u7528Python\u4f20\u53c2\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u547d\u4ee4\u884c\u53c2\u6570\u3001\u73af\u5883\u53d8\u91cf\u3001\u914d\u7f6e\u6587\u4ef6\u3001\u51fd\u6570\u53c2\u6570\u7b49\u3002\u547d\u4ee4\u884c\u53c2\u6570\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\uff0cPython\u901a\u8fc7sys.argv\u6216argparse\u6a21\u5757\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u3002<\/strong> \u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Linux\u4e2d\u4f7f\u7528Python\u8fdb\u884c\u53c2\u6570\u4f20\u9012\uff0c\u5e76\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\u7684\u5e94\u7528\u573a\u666f\u548c\u5177\u4f53\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u547d\u4ee4\u884c\u53c2\u6570\u4f20\u9012<\/p>\n<\/p>\n<p><p>\u5728Linux\u4e2d\uff0c\u547d\u4ee4\u884c\u662f\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u7684\u4e3b\u8981\u65b9\u5f0f\u4e4b\u4e00\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u8fd9\u4e9b\u53c2\u6570\u53ef\u4ee5\u5728\u811a\u672c\u6267\u884c\u65f6\u4f20\u9012\u7ed9Python\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528sys.argv<\/h3>\n<\/p>\n<p><p><code>sys.argv<\/code>\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u547d\u4ee4\u884c\u53c2\u6570\u3002\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u811a\u672c\u540d\u79f0\uff0c\u4ece\u7b2c\u4e8c\u4e2a\u5143\u7d20\u5f00\u59cb\u662f\u4f20\u9012\u7ed9\u811a\u672c\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>    # \u6253\u5370\u6240\u6709\u4f20\u5165\u7684\u53c2\u6570<\/p>\n<p>    print(&quot;Script name:&quot;, sys.argv[0])<\/p>\n<p>    for i, arg in enumerate(sys.argv[1:], start=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>\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u811a\u672c\u65f6\uff0c\u53ef\u4ee5\u4f20\u9012\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python script.py arg1 arg2 arg3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528argparse\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>argparse<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u52a0\u7075\u6d3b\u548c\u5f3a\u5927\u7684\u547d\u4ee4\u884c\u53c2\u6570\u89e3\u6790\u529f\u80fd\uff0c\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u53c2\u6570\u5b9a\u4e49\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;, 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;, const=sum, default=max, 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>\u6267\u884c\u65f6\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528<code>--sum<\/code>\u9009\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python script.py 1 2 3 4 5 --sum<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u73af\u5883\u53d8\u91cf\u4f20\u9012<\/p>\n<\/p>\n<p><p>\u73af\u5883\u53d8\u91cf\u662f\u4e00\u79cd\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u4f20\u9012\u53c2\u6570\u7684\u5e38\u89c1\u65b9\u5f0f\u3002Python\u53ef\u4ee5\u901a\u8fc7<code>os.environ<\/code>\u8bbf\u95ee\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528os.environ<\/h3>\n<\/p>\n<p><p><code>os.environ<\/code>\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u7528\u4e8e\u5b58\u50a8\u73af\u5883\u53d8\u91cf\u3002\u53ef\u4ee5\u901a\u8fc7\u952e\u8bbf\u95ee\u7279\u5b9a\u7684\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def main():<\/p>\n<p>    # \u83b7\u53d6\u73af\u5883\u53d8\u91cf<\/p>\n<p>    path = os.environ.get(&#39;PATH&#39;)<\/p>\n<p>    print(&quot;PATH environment variable:&quot;, path)<\/p>\n<p>    # \u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/p>\n<p>    os.environ[&#39;MY_VARIABLE&#39;] = &#39;some_value&#39;<\/p>\n<p>    print(&quot;MY_VARIABLE:&quot;, os.environ[&#39;MY_VARIABLE&#39;])<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">export MY_VARIABLE=some_value<\/p>\n<p>python script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u914d\u7f6e\u6587\u4ef6\u4f20\u9012<\/p>\n<\/p>\n<p><p>\u914d\u7f6e\u6587\u4ef6\u7528\u4e8e\u5b58\u50a8\u5e94\u7528\u7a0b\u5e8f\u7684\u53c2\u6570\u548c\u8bbe\u7f6e\uff0c\u5e38\u7528\u7684\u683c\u5f0f\u5305\u62ecINI\u3001JSON\u3001YAML\u7b49\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u6765\u8bfb\u53d6\u8fd9\u4e9b\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528configparser\u8bfb\u53d6INI\u6587\u4ef6<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import configparser<\/p>\n<p>def main():<\/p>\n<p>    config = configparser.ConfigParser()<\/p>\n<p>    config.read(&#39;config.ini&#39;)<\/p>\n<p>    # \u8bfb\u53d6\u914d\u7f6e<\/p>\n<p>    section = config[&#39;DEFAULT&#39;]<\/p>\n<p>    print(&quot;Default section key1:&quot;, section[&#39;key1&#39;])<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528json\u6a21\u5757\u8bfb\u53d6JSON\u6587\u4ef6<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>def main():<\/p>\n<p>    with open(&#39;config.json&#39;, &#39;r&#39;) as f:<\/p>\n<p>        config = json.load(f)<\/p>\n<p>    # \u8bfb\u53d6\u914d\u7f6e<\/p>\n<p>    print(&quot;Config key1:&quot;, config[&#39;key1&#39;])<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u51fd\u6570\u53c2\u6570\u4f20\u9012<\/p>\n<\/p>\n<p><p>\u5728\u7f16\u5199Python\u811a\u672c\u65f6\uff0c\u51fd\u6570\u53c2\u6570\u662f\u6700\u57fa\u672c\u7684\u53c2\u6570\u4f20\u9012\u65b9\u5f0f\u3002\u901a\u8fc7\u5728\u51fd\u6570\u5b9a\u4e49\u4e2d\u6307\u5b9a\u53c2\u6570\uff0c\u53ef\u4ee5\u5728\u8c03\u7528\u65f6\u4f20\u9012\u4e0d\u540c\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u51fd\u6570\u53c2\u6570<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def process_data(data, multiplier=1):<\/p>\n<p>    return [x * multiplier for x in data]<\/p>\n<p>def main():<\/p>\n<p>    data = [1, 2, 3, 4]<\/p>\n<p>    # \u76f4\u63a5\u4f20\u9012\u53c2\u6570<\/p>\n<p>    print(&quot;Processed data:&quot;, process_data(data, multiplier=2))<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Linux\u4e2d\u4f7f\u7528Python\u4f20\u9012\u53c2\u6570\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u3002<strong>\u547d\u4ee4\u884c\u53c2\u6570\u9002\u7528\u4e8e\u7b80\u5355\u7684\u811a\u672c\u6267\u884c\uff0c\u73af\u5883\u53d8\u91cf\u9002\u7528\u4e8e\u9700\u8981\u4e0e\u7cfb\u7edf\u73af\u5883\u8fdb\u884c\u4ea4\u4e92\u7684\u573a\u666f\uff0c\u914d\u7f6e\u6587\u4ef6\u9002\u7528\u4e8e\u590d\u6742\u5e94\u7528\u7684\u914d\u7f6e\u7ba1\u7406\uff0c\u800c\u51fd\u6570\u53c2\u6570\u5219\u662f\u65e5\u5e38\u7f16\u7a0b\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u4e4b\u4e00\u3002<\/strong> \u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u53c2\u6570\u4f20\u9012\u6280\u672f\uff0c\u53ef\u4ee5\u63d0\u9ad8Python\u811a\u672c\u7684\u53ef\u6269\u5c55\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Linux\u73af\u5883\u4e0b\uff0c\u5982\u4f55\u5728Python\u811a\u672c\u4e2d\u63a5\u6536\u547d\u4ee4\u884c\u53c2\u6570\uff1f<\/strong><br \/>\u5728Linux\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>sys<\/code>\u6a21\u5757\u4e2d\u7684<code>argv<\/code>\u5217\u8868\u6765\u63a5\u6536\u547d\u4ee4\u884c\u53c2\u6570\u3002<code>argv[0]<\/code>\u662f\u811a\u672c\u540d\u79f0\uff0c\u4ece<code>argv[1]<\/code>\u5f00\u59cb\u662f\u4f20\u9012\u7ed9\u811a\u672c\u7684\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u8fd0\u884c<code>python script.py arg1 arg2<\/code>\u65f6\uff0c<code>argv[1]<\/code>\u5c06\u662f<code>arg1<\/code>\uff0c<code>argv[2]<\/code>\u662f<code>arg2<\/code>\u3002\u901a\u8fc7\u904d\u5386<code>argv<\/code>\uff0c\u60a8\u53ef\u4ee5\u5904\u7406\u4f20\u5165\u7684\u6240\u6709\u53c2\u6570\u3002<\/p>\n<p><strong>Python\u4e2d\u662f\u5426\u6709\u66f4\u65b9\u4fbf\u7684\u65b9\u5f0f\u6765\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\uff1f<\/strong><br \/>\u786e\u5b9e\u6709\u3002\u4f7f\u7528<code>argparse<\/code>\u6a21\u5757\u53ef\u4ee5\u66f4\u7b80\u6d01\u5730\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u3002<code>argparse<\/code>\u5141\u8bb8\u60a8\u5b9a\u4e49\u53c2\u6570\u7684\u7c7b\u578b\u3001\u5e2e\u52a9\u4fe1\u606f\u548c\u9ed8\u8ba4\u503c\u7b49\u529f\u80fd\uff0c\u4f7f\u5f97\u811a\u672c\u7684\u4f7f\u7528\u66f4\u52a0\u53cb\u597d\u3002\u901a\u8fc7\u521b\u5efa<code>ArgumentParser<\/code>\u5bf9\u8c61\uff0c\u60a8\u53ef\u4ee5\u6dfb\u52a0\u6240\u9700\u7684\u53c2\u6570\uff0c\u7136\u540e\u8c03\u7528<code>parse_args()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u89e3\u6790\u540e\u7684\u53c2\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u811a\u672c\u4e2d\u4f20\u9012\u591a\u4e2a\u53c2\u6570\u5e76\u786e\u4fdd\u5b83\u4eec\u7684\u987a\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f20\u9012\u591a\u4e2a\u53c2\u6570\u65f6\uff0c\u987a\u5e8f\u901a\u5e38\u662f\u7531\u60a8\u5728\u547d\u4ee4\u884c\u4e2d\u63d0\u4f9b\u53c2\u6570\u7684\u987a\u5e8f\u51b3\u5b9a\u7684\u3002\u4e3a\u4e86\u786e\u4fdd\u987a\u5e8f\u6b63\u786e\uff0c\u60a8\u53ef\u4ee5\u5728<code>argparse<\/code>\u4e2d\u4f7f\u7528\u4f4d\u7f6e\u53c2\u6570\uff0c\u6216\u8005\u4f7f\u7528\u5173\u952e\u5b57\u53c2\u6570\u3002\u4f4d\u7f6e\u53c2\u6570\u4f1a\u6839\u636e\u987a\u5e8f\u8fdb\u884c\u5339\u914d\uff0c\u800c\u5173\u952e\u5b57\u53c2\u6570\u53ef\u4ee5\u5728\u4efb\u4f55\u987a\u5e8f\u4e0b\u63d0\u4f9b\uff0c\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n<p><strong>\u5728Linux\u7ec8\u7aef\u4e2d\uff0c\u5982\u4f55\u68c0\u67e5\u4f20\u9012\u7ed9Python\u811a\u672c\u7684\u53c2\u6570\u662f\u5426\u6709\u6548\uff1f<\/strong><br \/>\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6765\u68c0\u67e5\u53c2\u6570\u7684\u6709\u6548\u6027\u3002\u901a\u8fc7<code>len(sys.argv)<\/code>\u6216<code>argparse<\/code>\u7684\u529f\u80fd\uff0c\u60a8\u53ef\u4ee5\u786e\u4fdd\u4f20\u9012\u4e86\u6b63\u786e\u6570\u91cf\u7684\u53c2\u6570\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u5757\u6765\u6355\u83b7\u6f5c\u5728\u7684\u7c7b\u578b\u9519\u8bef\u6216\u503c\u9519\u8bef\uff0c\u786e\u4fdd\u4f20\u5165\u7684\u53c2\u6570\u7b26\u5408\u9884\u671f\u7684\u683c\u5f0f\u548c\u7c7b\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Linux\u4e2d\u4f7f\u7528Python\u4f20\u53c2\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u547d\u4ee4\u884c\u53c2\u6570\u3001\u73af\u5883\u53d8\u91cf\u3001\u914d\u7f6e\u6587\u4ef6\u3001\u51fd\u6570\u53c2\u6570\u7b49\u3002\u547d\u4ee4\u884c\u53c2\u6570\u662f [&hellip;]","protected":false},"author":3,"featured_media":970920,"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\/970914"}],"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=970914"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/970914\/revisions"}],"predecessor-version":[{"id":970921,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/970914\/revisions\/970921"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/970920"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=970914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=970914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=970914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}