{"id":17,"date":"2023-07-15T19:46:33","date_gmt":"2023-07-15T11:46:33","guid":{"rendered":"http:\/\/www.weigot.com\/?p=17"},"modified":"2023-07-15T20:24:54","modified_gmt":"2023-07-15T12:24:54","slug":"%e8%bf%9b%e7%a8%8b%e5%ad%90%e7%a8%8b%e5%ba%8f%e5%ad%90%e7%a8%8b%e5%ba%8f%e6%88%96%e8%80%85%e5%ad%90%e8%bf%9b%e7%a8%8b%ef%bc%8c%e5%8f%af%e4%bb%a5%e4%bd%bf%e7%94%a8-subprocess-%e6%a8%a1","status":"publish","type":"post","link":"http:\/\/www.weigot.com\/index.php\/2023\/07\/15\/%e8%bf%9b%e7%a8%8b%e5%ad%90%e7%a8%8b%e5%ba%8f%e5%ad%90%e7%a8%8b%e5%ba%8f%e6%88%96%e8%80%85%e5%ad%90%e8%bf%9b%e7%a8%8b%ef%bc%8c%e5%8f%af%e4%bb%a5%e4%bd%bf%e7%94%a8-subprocess-%e6%a8%a1\/","title":{"rendered":"\u8fdb\u7a0b\u5b50\u7a0b\u5e8f\u5b50\u7a0b\u5e8f\u6216\u8005\u5b50\u8fdb\u7a0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u00a0subprocess\u00a0\u6a21\u5757"},"content":{"rendered":"\n<p>\u5728 Python \u4e2d\u542f\u52a8\u4e00\u4e2a\u5b50\u7a0b\u5e8f\u6216\u8005\u5b50\u8fdb\u7a0b\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528&nbsp;<code>subprocess<\/code>&nbsp;\u6a21\u5757\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<p>pythonCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import subprocess\n\n# \u542f\u52a8\u5b50\u7a0b\u5e8f\u5e76\u7b49\u5f85\u5176\u7ed3\u675f\nsubprocess.run(&#91;'python', 'my_script.py'])\n\n# \u542f\u52a8\u5b50\u7a0b\u5e8f\u5e76\u5728\u540e\u53f0\u8fd0\u884c\nsubprocess.Popen(&#91;'python', 'my_script.py'])\n<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528&nbsp;<code>subprocess.run()<\/code>&nbsp;\u51fd\u6570\u6765\u542f\u52a8\u4e00\u4e2a\u540d\u4e3a&nbsp;<code>my_script.py<\/code>&nbsp;\u7684 Python \u5b50\u7a0b\u5e8f\uff0c\u5e76\u7b49\u5f85\u5176\u7ed3\u675f\u3002\u6211\u4eec\u8fd8\u4f7f\u7528&nbsp;<code>subprocess.Popen()<\/code>&nbsp;\u51fd\u6570\u6765\u542f\u52a8\u540c\u6837\u7684\u5b50\u7a0b\u5e8f\uff0c\u4f46\u662f\u5728\u540e\u53f0\u8fd0\u884c\u3002\u8fd9\u4e24\u4e2a\u51fd\u6570\u90fd\u5c06\u5b50\u7a0b\u5e8f\u4f5c\u4e3a\u5217\u8868\u4f20\u9012\uff0c\u5e76\u4f7f\u7528\u7b2c\u4e00\u4e2a\u53c2\u6570\u6307\u5b9a\u8981\u6267\u884c\u7684\u7a0b\u5e8f\u6216\u8005\u547d\u4ee4\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u60f3\u5728\u5b50\u7a0b\u5e8f\u4e2d\u4f20\u9012\u53c2\u6570\uff0c\u53ef\u4ee5\u5c06\u5b83\u4eec\u4f5c\u4e3a\u5217\u8868\u4f20\u9012\u7ed9&nbsp;<code>run()<\/code>&nbsp;\u6216\u8005&nbsp;<code>Popen()<\/code>&nbsp;\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<p>pythonCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import subprocess\n\n# \u542f\u52a8\u5b50\u7a0b\u5e8f\u5e76\u4f20\u9012\u53c2\u6570\nsubprocess.run(&#91;'python', 'my_script.py', '--arg1', 'value1', '--arg2', 'value2'])\n\n# \u542f\u52a8\u5b50\u7a0b\u5e8f\u5e76\u5728\u540e\u53f0\u8fd0\u884c\nsubprocess.Popen(&#91;'python', 'my_script.py', '--arg1', 'value1', '--arg2', 'value2'])\n<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5c06&nbsp;<code>--arg1<\/code>&nbsp;\u548c&nbsp;<code>--arg2<\/code>&nbsp;\u53c2\u6570\u4f20\u9012\u7ed9\u5b50\u7a0b\u5e8f\uff0c\u5e76\u5c06\u5b83\u4eec\u4f5c\u4e3a\u5217\u8868\u4f20\u9012\u7ed9&nbsp;<code>run()<\/code>&nbsp;\u6216\u8005&nbsp;<code>Popen()<\/code>&nbsp;\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u60f3\u6355\u83b7\u5b50\u7a0b\u5e8f\u7684\u8f93\u51fa\uff0c\u53ef\u4ee5\u4f7f\u7528&nbsp;<code>subprocess.run()<\/code>&nbsp;\u51fd\u6570\u7684&nbsp;<code>capture_output<\/code>&nbsp;\u53c2\u6570\uff0c\u6216\u8005\u4f7f\u7528&nbsp;<code>subprocess.Popen()<\/code>&nbsp;\u51fd\u6570\u7684&nbsp;<code>stdout<\/code>&nbsp;\u548c&nbsp;<code>stderr<\/code>&nbsp;\u53c2\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<p>pythonCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import subprocess\n\n# \u542f\u52a8\u5b50\u7a0b\u5e8f\u5e76\u6355\u83b7\u8f93\u51fa\nresult = subprocess.run(&#91;'python', 'my_script.py'], capture_output=True)\nprint(result.stdout.decode())\n<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528&nbsp;<code>capture_output<\/code>&nbsp;\u53c2\u6570\u6765\u6355\u83b7\u5b50\u7a0b\u5e8f\u7684\u8f93\u51fa\uff0c\u5e76\u4f7f\u7528&nbsp;<code>result.stdout.decode()<\/code>&nbsp;\u51fd\u6570\u5c06\u8f93\u51fa\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32\u5e76\u6253\u5370\u5230\u63a7\u5236\u53f0\u4e0a\u3002<\/p>\n\n\n\n<p><code>subprocess.run()<\/code>&nbsp;\u51fd\u6570\u53ef\u4ee5\u63a5\u6536\u591a\u4e2a\u53c2\u6570\uff0c\u5176\u4e2d\u5305\u62ec\u8981\u6267\u884c\u7684\u547d\u4ee4\u548c\u547d\u4ee4\u884c\u53c2\u6570\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528&nbsp;<code>subprocess.run()<\/code>&nbsp;\u51fd\u6570\u4f20\u9012\u53c2\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p>pythonCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import subprocess\n\n# \u5b9a\u4e49\u8981\u6267\u884c\u7684\u547d\u4ee4\u548c\u53c2\u6570\ncommand = &#91;'python', 'my_script.py', 'arg1', 'arg2', 'arg3']\n\n# \u4f7f\u7528 subprocess.run() \u51fd\u6570\u6267\u884c\u547d\u4ee4\nresult = subprocess.run(command, capture_output=True)\n\n# \u6253\u5370\u547d\u4ee4\u8f93\u51fa\u7ed3\u679c\nprint(result.stdout.decode())\n<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86&nbsp;<code>subprocess.run()<\/code>&nbsp;\u51fd\u6570\u6765\u6267\u884c\u4e00\u4e2a\u540d\u4e3a&nbsp;<code>my_script.py<\/code>&nbsp;\u7684 Python \u811a\u672c\uff0c\u5e76\u4f20\u9012\u4e86\u4e09\u4e2a\u547d\u4ee4\u884c\u53c2\u6570&nbsp;<code>arg1<\/code>\u3001<code>arg2<\/code>&nbsp;\u548c&nbsp;<code>arg3<\/code>\u3002\u6211\u4eec\u5c06\u8fd9\u4e9b\u547d\u4ee4\u548c\u53c2\u6570\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u5e76\u5c06\u5176\u4f20\u9012\u7ed9&nbsp;<code>subprocess.run()<\/code>&nbsp;\u51fd\u6570\u3002<\/p>\n\n\n\n<p><code>subprocess.run()<\/code>&nbsp;\u51fd\u6570\u8fd8\u6709\u5176\u4ed6\u53ef\u9009\u53c2\u6570\uff0c\u4f8b\u5982&nbsp;<code>capture_output<\/code>\uff0c\u7528\u4e8e\u6355\u83b7\u547d\u4ee4\u7684\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u8f93\u51fa\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u4f7f\u7528\u8fd9\u4e9b\u53c2\u6570\u6765\u81ea\u5b9a\u4e49&nbsp;<code>subprocess.run()<\/code>&nbsp;\u51fd\u6570\u7684\u884c\u4e3a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python \u4e2d\u542f\u52a8\u4e00\u4e2a\u5b50\u7a0b\u5e8f\u6216\u8005\u5b50\u8fdb\u7a0b\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528&nbsp;subprocess&nbsp;\u6a21\u5757\u3002\u4ee5\u4e0b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-17","post","type-post","status-publish","format-standard","hentry","category-suibianjilu"],"_links":{"self":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/17","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/comments?post=17"}],"version-history":[{"count":2,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/17\/revisions"}],"predecessor-version":[{"id":20,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/17\/revisions\/20"}],"wp:attachment":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/media?parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/categories?post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/tags?post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}