{"rsdb":{"rid":"395126","subhead":"","postdate":"0","aid":"272506","fid":"54","uid":"1","topic":"1","content":"

\u4e00\u4e2a\u7b80\u5355\u7684Python\u793a\u4f8b<\/strong><\/p>

\u8ba9\u6211\u4eec\u521b\u5efa\u7b2c\u4e00\u4e2a\u7a0b\u5e8f\u4ee5\u5217\u51fa\u5f53\u524d\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u53ef\u7528\u6587\u4ef6\u3002 \u60a8\u53ef\u4ee5\u6dfb\u52a0\u4efb\u610f\u6570\u91cf\u7684\u4ee5\u9017\u53f7\uff08\uff0c\uff09\u5206\u9694\u7684\u547d\u4ee4\u884c\u53c2\u6570\u3002<\/p>

#!\/usr\/bin\/python<\/a>3
import subprocess
subprocess.call(["ls", "-l"])<\/p>

\"\u5982\u4f55\u5728Python\u4e2d\u6267\u884c\u5916\u90e8\u547d\u4ee4\"<\/p>

import\u8bed\u53e5\u7528\u4e8e\u4ecePython\u6807\u51c6\u5e93\u52a0\u8f7d\u5b50\u6d41\u7a0b\u6a21\u5757<\/p>

call\u662f\u5b50\u6d41\u7a0b\u6a21\u5757\u7684\u529f\u80fd\uff0c\u7528\u4e8e\u6267\u884c\u5916\u90e8\u547d\u4ee4<\/p>

\u6ca1\u6709\u6362\u884c\u7684Python\u6253\u5370<\/strong><\/p>

Python\u547d\u4ee4\u4ee5\u65b0\u884c\u8f93\u51fa\u201c \\n\u201d\u7ec8\u6b62\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 end=\u201d\u201d \u8986\u76d6\u65b0\u884c\u8f93\u51fa\uff0c\u8fd9\u610f\u5473\u7740\u4e0b\u4e00\u4e2a\u547d\u4ee4\u8f93\u51fa\u5c06\u5728\u540c\u4e00\u884c\u4e0a\u3002 \u8bf7\u53c2\u89c1\u4ee5\u4e0b\u793a\u4f8b\u3002<\/p>

#!\/usr\/bin\/python<\/a>3
 
import subprocess
 
print("\\nToday is ", end="")
subprocess.call(["date","+%D"])<\/p>

\"\u5982\u4f55\u5728Python\u4e2d\u6267\u884c\u5916\u90e8\u547d\u4ee4\"<\/p>

\u5177\u6709Shell\u6269\u5c55\u529f\u80fd\u7684Python\u793a\u4f8b<\/strong><\/p>

\u9ed8\u8ba4\u7684subprocess.call\u4e0d\u4f7f\u7528shell\u6269\u5c55\u901a\u914d\u7b26\u6216\u6267\u884c\u547d\u4ee4\u66ff\u6362\u3002 \u53ef\u4ee5\u901a\u8fc7\u4e3ashell\u53c2\u6570\u4f20\u9012True\u503c\u6765\u8986\u76d6\u6b64\u8bbe\u7f6e\u3002\u8bf7\u8bb0\u4f4f\uff0c\u4f7f\u7528shell=True\u53ef\u80fd\u4f1a\u56e0\u4e3a\u6267\u884c\u6709\u5bb3\u7684\u547d\u4ee4\u800c\u9020\u6210\u7cfb\u7edf\u7684\u5b89\u5168\u95ee\u9898\u3002<\/p>

#!\/usr\/bin\/python3
 
import subprocess
 
# \u5728\u4e0d\u4f7f\u7528shell\u6269\u5c55\u7684\u60c5\u51b5\u4e0b\u6267\u884c\u547d\u4ee4
subprocess.call(['echo', 'Welcome $USER'])
 
# \u4f7f\u7528Shell\u6269\u5c55\u6267\u884c\u547d\u4ee4
subprocess.call('echo Welcome $USER', shell=True)<\/p>

\"\u5982\u4f55\u5728Python\u4e2d\u6267\u884c\u5916\u90e8\u547d\u4ee4\"<\/p>

\u60a8\u53ef\u4ee5\u5728\u4e0a\u9762\u7684\u8f93\u51fa\u4e2d\u770b\u5230\uff0c\u7b2c\u4e00\u4e2a\u547d\u4ee4\u6253\u5370\u4e86\u4e00\u4e2a\u53d8\u91cf\u540d\uff0c\u56e0\u4e3a\u5b83\u662f\u5728\u6ca1\u6709shell\u6269\u5c55\u7684\u60c5\u51b5\u4e0b\u6267\u884c\u7684\u3002 \u4f7f\u7528shell\u6269\u5c55\u6267\u884c\u7684\u7b2c\u4e8c\u6761\u547d\u4ee4\u83b7\u53d6USER\u73af\u5883\u53d8\u91cf\u7684\u503c\u3002 \u53e6\u5916\uff0c\u60a8\u53ef\u4ee5\u770b\u5230\u6574\u4e2a\u547d\u4ee4\u73b0\u5728\u4f5c\u4e3a\u5b57\u7b26\u4e32\u800c\u4e0d\u662f\u4f5c\u4e3a\u5b57\u7b26\u4e32\u5217\u8868\u4f20\u9012\u3002<\/p>

\u5176\u4ed6\u6709\u7528\u7684Python\u793a\u4f8b<\/strong><\/p>

\u901a\u8fc7\u5c06\u957f\u547d\u4ee4\u5b58\u50a8\u5728\u53d8\u91cf\u4e2d\u7136\u540e\u6267\u884c\u5b83\uff0c\u53ef\u4ee5\u4f7f\u811a\u672c\u7684\u53ef\u8bfb\u6027\u548c\u7f8e\u89c2\u6027\u66f4\u9ad8\u3002<\/p>

#!\/usr\/bin\/python3
 
import subprocess
 
cmd = "grep -Re 'Fatal' \/var\/log\/ > linuxidc.txt"
subprocess.call(cmd, shell=True)<\/p>

\"\u5982\u4f55\u5728Python\u4e2d\u6267\u884c\u5916\u90e8\u547d\u4ee4\"<\/p>

\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528subprocess.getoutput\u5c06\u4efb\u4f55\u547d\u4ee4\u7684\u8f93\u51fa\u5b58\u50a8\u5230\u53d8\u91cf\u4e2d\u3002 \u5982\u679c\u751f\u6210\u4efb\u4f55\u9519\u8bef\u6d88\u606f\uff0c\u5b83\u4e5f\u5c06\u5b58\u50a8\u3002<\/p>

#!\/usr\/bin\/python3
 
import subprocess
 
print("The output of 'pwd' command is:", flush=True)
 
output = subprocess.getoutput('pwd')
print(output)<\/p>

\"\u5982\u4f55\u5728Python\u4e2d\u6267\u884c\u5916\u90e8\u547d\u4ee4\"<\/p>","orderid":"0","title":"\u5982\u4f55\u5728Python\u4e2d\u6267\u884c\u5916\u90e8\u547d\u4ee4","smalltitle":"","mid":"0","fname":"linux\u7f16\u7a0b\u57fa\u7840","special_id":"0","bak_id":"0","info":"0","hits":"550","pages":"1","comments":"0","posttime":"2023-07-26 08:18:31","list":"1690330711","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"\u4f55\u5728<\/A> Python<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"119.59.235.169","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"\u5982\u4f55\u5728Python\u4e2d\u6267\u884c\u5916\u90e8\u547d\u4ee4","lastview":"1715493340","digg_num":"1035","digg_time":"1715843517","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}