Python 執行 shell 獲取輸出結果的例項

2022-10-04 18:21:11 字數 3020 閱讀 8824

首先使用內建模組os.

>>> import os

>>> code = os.system("pwd && sleep 2")

# /user/zhipeng

>>> print code

# 0問題是 os.system 只能獲取到結束狀態

使用內建模組 subprocess

>>> import subprocess

>>> subprocess.popen("pwd && sleep 2", shell=true, cwd="/home")

# # /home

>>> sub = subpro程式設計客棧cess.popen("pwd && sleep 2", shell=true, stdout=subprcess.pipe)

>>> sub.wait()

>>> print sub.stdou程式設計客棧t.read()

# /user/zhipeng

subprocess.popen還支援一些別的引數

bufsize,executable=none, stdin=none, stdout=none, stderr=none

preexec_fn=none, close_fds=false, shell=false, cwd=none, env=none

universal_newlines=false, startupinfo=none, creationflags=0

使用第三方模組 sh

# pip install sh

>>> from sh import ifconfig

>>> print ifconfig("eth0")

>>> from sh import bash

>>> bash("pwd")

traceback (most recent call last):

file "", line 1, in

file "/library/python/2.7/site-packages/sh.py", line 1021, in __call__

return runningcommand(cmd, call_args, stdin, stdout, stderr)

file "/library/python/2.7/site-packages/sh.py", line 486, in __init__

self.wait()

file "/library/python/2.7/site-packages/sh.py", line 500, in wait

self.handle_command_exit_code(exit_code)

file "/library/python/2.7/site-packages/sh.py", line 516, in handle_command_exit_code

raise exc(self.ran, self.process.stdout, self.process.stderr)

sh.errorreturncode_126:

ran: '/bin/bash ls'

stdout:

stderr:

/bin/ls: /bin/ls: cannot execute binary file

# 不能這麼用

>>> from sh import ls

>>> ls()

# hello.txt 1.txt

# ls -al

>>> ls(a=true, l=true)

# ls(al=true) 是不可以的

這操作太複雜了, 專案中使用也太糟心了, 也沒有辦法多個命令同時用.不過可以用別的方式代替

# bash -c command 可以很好的解決這個問題

# bash -c "sleep 1 && pwd"

>>> result = bash(c="pwd", _timeout=1, _cwd="/home")

>>> print result

# -rw-r--r--@ 1 zhipeng staff 0 10 13 18:30 hello.txt

# -rw-r--r--@ 1 zhipeng staff 0 10 13 18:30 1.txt

>>> result = bash(c="pwd", _timeout=1, _cwd="/")

>>> print result

# />>> bash(c="pwd && sleep 2", _timeout=1)

traceback (most recent call last):

file "", line 1, in

file "/library/python/2.7/site-packages/sh.py", line 1021, in __call__

return runningcomhlhatqmand(cmd, call_args, stdin, stdout, stderr)

file "/library/python/2.7/site-packages/sh.py", line 486, in __init__

self.wait()

file "/library/python/2.7/site-packages/sh.py", line 498, in wait

raise timeoutexception(-exit_code)

sh.timeoutexception

引數裡面可以新增非命令引數. 需要以_開頭, 例如上面的_timeout, _cwd. 詳見sh.py 原始碼

還支援以下引數

internal_bufsize, err_bufsize, tee, done, in, decode_errors, tty_in,

out, cwd, timeout_signal, bg, timeout, with, ok_code, err, env, no_out,

參考:本文標題: python 執行 shell 獲取輸出結果的例項

本文位址: /jiaoben/python/249196.html

python執行shell命令

在此比較一下兩種方法執行系統命令的方法,以方便於日後運用 1.os.system system command exit status execute the command a string in a subshell.僅僅在乙個子終端執行系統命令,而不能獲取命令執行後的返回資訊.os.syste...

python的執行shell命令

os.system cat proc cpuinfo 返回的是執行的結果,1或者是其他 output os.popen cat proc cpuinfo print output.read 通過 os.popen 返回的是 file read 的物件,對其進行讀取 read 的操作可以看到執行的輸出...

shell中執行python檔案

python中想在shell中呼叫乙個test.py檔案裡面的方法。test.py檔案裡面的內容如下 python view plain copy print?deflistfea print this is myself deflistfeat fea print this is fea defl...