{"id":4578,"date":"2013-01-21T09:29:17","date_gmt":"2013-01-21T01:29:17","guid":{"rendered":"http:\/\/jpuyy.com\/?p=4578"},"modified":"2014-02-25T18:25:41","modified_gmt":"2014-02-25T10:25:41","slug":"python-execute-shell-command","status":"publish","type":"post","link":"https:\/\/jpuyy.com\/?p=4578","title":{"rendered":"python\u6267\u884cshell\u547d\u4ee4"},"content":{"rendered":"<p>\u5728 python for unix and linux system administraton\u4e00\u4e66\u4e2d\uff0c\u6709\u8fd9\u4e48\u4e00\u53e5\u8bdd\uff1a<\/p>\n<blockquote><p>You can run any shell command in Python just as it would be run with Bash.<\/p><\/blockquote>\n<p>\u4f60\u53ef\u4ee5\u7528python\u6267\u884cbash\u4e2d\u53ef\u4ee5\u6267\u884c\u7684\u4efb\u4f55\u547d\u4ee4\u3002<\/p>\n<p>\u663e\u793a\/tmp\/\u4fe1\u606f\uff08\u4e24\u79cd\u6267\u884cbash\u547d\u4ee4\u7684\u65b9\u5f0f\uff09<\/p>\n<pre>#!\/usr\/bin\/env python\r\nimport subprocess\r\nsubprocess.call([\"ls\",\"-l\",\"\/tmp\/\"])\r\nsubprocess.call(\"df -l \/tmp\/\", shell=True)<\/pre>\n<p>\u5c06\u8f93\u51fa\u8d4b\u7ed9\u6307\u5b9a\u53d8\u91cf<\/p>\n<pre>output = subprocess.check_output('ls', shell=True)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 python for unix and linux system administraton\u4e00\u4e66\u4e2d\uff0c\u6709\u8fd9\u4e48\u4e00\u53e5\u8bdd\uff1a You can run any shell command in Python just as it would be run with Bash. \u4f60\u53ef\u4ee5\u7528python\u6267\u884cbash\u4e2d\u53ef\u4ee5\u6267\u884c\u7684\u4efb\u4f55\u547d\u4ee4\u3002 \u663e\u793a\/tmp\/\u4fe1\u606f\uff08\u4e24\u79cd\u6267\u884cbash\u547d\u4ee4\u7684\u65b9\u5f0f\uff09 #!\/usr\/bin\/env python import subprocess subprocess.call([&#8220;ls&#8221;,&#8221;-l&#8221;,&#8221;\/tmp\/&#8221;]) subprocess.call(&#8220;df -l \/tmp\/&#8221;, shell=True) \u5c06\u8f93\u51fa\u8d4b\u7ed9\u6307\u5b9a\u53d8\u91cf output = subprocess.check_output(&#8216;ls&#8217;, shell=True)<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[23],"class_list":["post-4578","post","type-post","status-publish","format-standard","hentry","category-python","tag-summary"],"_links":{"self":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/4578","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4578"}],"version-history":[{"count":7,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/4578\/revisions"}],"predecessor-version":[{"id":6152,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/4578\/revisions\/6152"}],"wp:attachment":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}