本篇文章給大家詳細講述了python中使用subprocess批量執行linux命令的方法,有興趣的朋友參考學習下。
可以執行shell指令的相關模組和函式有:
os.system
os.spawn
os.popen --廢棄
popen --廢棄
subprocess
call
>>> import subprocess >>> ret = subprocess.call(["ls", "-l"], shell=False) total 4684 -rw-r--r-- 1 root root 454 May 5 12:20 aa.py -rw-r--r-- 1 root root 0 May 8 16:51 aa.txt -rw-r--r-- 1 root root 4783286 Apr 11 16:39 DockerToolbox.exe -rw-r--r-- 1 root root 422 May 5 12:20 ip_info.txt -rw-r--r-- 1 root root 718 Apr 19 10:52 my.cnf >>> ret = subprocess.call("ls -l", shell=True) total 4684 -rw-r--r-- 1 root root 454 May 5 12:20 aa.py -rw-r--r-- 1 root root 0 May 8 16:51 aa.txt -rw-r--r-- 1 root root 4783286 Apr 11 16:39 DockerToolbox.exe -rw-r--r-- 1 root root 422 May 5 12:20 ip_info.txt -rw-r--r-- 1 root root 718 Apr 19 10:52 my.cnf >>> print(ret) 0
check_call
#
>>> subprocess.check_call(["ls", "-l"]) total 4684 -rw-r--r-- 1 root root 454 May 5 12:20 aa.py -rw-r--r-- 1 root root 0 May 8 16:51 aa.txt -rw-r--r-- 1 root root 4783286 Apr 11 16:39 DockerToolbox.exe -rw-r--r-- 1 root root 422 May 5 12:20 ip_info.txt -rw-r--r-- 1 root root 718 Apr 19 10:52 my.cnf 0 >>> subprocess.check_call("exit 1", shell=True) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/python3.5/lib/python3.5/subprocess.py", line 581, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
check_output
>>> subprocess.check_output(["echo", "Hello World!"]) b'Hello World!\n' >>> subprocess.check_output("exit 1", shell=True) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/python3.5/lib/python3.5/subprocess.py", line 626, in check_output **kwargs).stdout File "/usr/local/python3.5/lib/python3.5/subprocess.py", line 708, in run output=stdout, stderr=stderr) subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
subprocess.Popen(...)
>>> import subprocess >>> ret1 = subprocess.Popen(["mkdir","t1"]) >>> ret2 = subprocess.Popen("mkdir t2", shell=True) >>> print(ret1) <subprocess.Popen object at 0x7f4d7609dd30> >>> print(ret2) <subprocess.Popen object at 0x7f4d7609dc18>
輸入進行某一環境,依賴再輸入,如:python
#>>> import subprocess >>> obj = subprocess.Popen("mkdir t3", shell=True, cwd='/tmp/',) >>> import subprocess >>> obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) >>> obj.stdin.write("print(1)\n") 9 >>> obj.stdin.write("print(2)") 8 >>> obj.stdin.close() >>> cmd_out = obj.stdout.read() >>> obj.stdout.close() >>> cmd_error = obj.stderr.read() >>> obj.stderr.close() >>> print(cmd_out) 1 2 >>> print(cmd_error)
>>> import subprocess >>> >>> obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) >>> obj.stdin.write("print(1)\n") 9 >>> obj.stdin.write("print(2)") 8 >>> >>> out_error_list = obj.communicate() >>> print(out_error_list) ('1\n2\n', '')
#################### #######
>>> obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) >>> out_error_list = obj.communicate('print("hello")') >>> print(out_error_list) ('hello\n', '')
以上是python中subprocess批次執行linux指令的詳細內容。更多資訊請關注PHP中文網其他相關文章!