Python 運(yùn)行 shell 獲取輸出結(jié)果的實(shí)例
首先使用內(nèi)置模塊os.
>>> import os
>>> code = os.system("pwd && sleep 2")
# /User/zhipeng
>>> print code
# 0
問(wèn)題是 os.system 只能獲取到結(jié)束狀態(tài)
使用內(nèi)置模塊 subprocess
>>> import subprocess
>>> subprocess.Popen("pwd && sleep 2", shell=True, cwd="/home")
# <subprocess.Popen object at 0x106498310>
# /home
>>> sub = subprocess.Popen("pwd && sleep 2", shell=True, stdout=subprcess.PIPE)
>>> sub.wait()
>>> print sub.stdout.read()
# /User/zhipeng
subprocess.Popen還支持一些別的參數(shù) 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 "<stdin>", line 1, in <module>
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) 是不可以的
這操作太復(fù)雜了, 項(xiàng)目中使用也太糟心了, 也沒(méi)有辦法多個(gè)命令同時(shí)用.不過(guò)可以用別的方式代替
# bash -c command 可以很好的解決這個(gè)問(wèn)題 # 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 "<stdin>", line 1, in <module> 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 498, in wait raise TimeoutException(-exit_code) sh.TimeoutException
參數(shù)里面可以添加非命令參數(shù). 需要以_開頭, 例如上面的_timeout, _cwd. 詳見sh.py 源碼 還支持以下參數(shù) internal_bufsize, err_bufsize, tee, done, in, decode_errors, tty_in, out, cwd, timeout_signal, bg, timeout, with, ok_code, err, env, no_out,
參考:
https://github.com/amoffat/sh/blob/master/sh.py
https://github.com/amoffat/sh
以上這篇Python 運(yùn)行 shell 獲取輸出結(jié)果的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
virtualenv隔離Python環(huán)境的問(wèn)題解析
virtualenv為應(yīng)用提供了隔離的Python運(yùn)行環(huán)境,解決了不同應(yīng)用間多版本的沖突問(wèn)題,這篇文章主要介紹了virtualenv隔離Python環(huán)境,需要的朋友可以參考下2022-06-06
學(xué)會(huì)使用Python?Configparser處理ini文件模塊
這篇文章主要為大家介紹了使用Python?Configparser處理ini文件模塊的學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Python定時(shí)任務(wù)APScheduler安裝及使用解析
這篇文章主要介紹了Python定時(shí)任務(wù)APScheduler安裝及使用解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
Python名片管理系統(tǒng)+猜拳小游戲案例實(shí)現(xiàn)彩(色控制臺(tái)版)
這篇文章主要介紹了Python名片管理系統(tǒng)+猜拳小游戲案例實(shí)現(xiàn)彩(色控制臺(tái)版),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-08-08
Pandas實(shí)現(xiàn)轉(zhuǎn)換產(chǎn)生新列的項(xiàng)目實(shí)踐
本文主要介紹了Pandas實(shí)現(xiàn)轉(zhuǎn)換產(chǎn)生新列,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-12-12
如何實(shí)現(xiàn)python爬蟲爬取視頻時(shí)實(shí)現(xiàn)實(shí)時(shí)進(jìn)度條顯示
這篇文章主要介紹了如何實(shí)現(xiàn)python爬蟲爬取視頻時(shí)實(shí)現(xiàn)實(shí)時(shí)進(jìn)度條顯示,在爬取并下載網(wǎng)頁(yè)上的視頻的時(shí)候,我們需要實(shí)時(shí)進(jìn)度條,這可以幫助我們更直觀的看到視頻的下載進(jìn)度。文章圍繞主題展開更多內(nèi)容,需要的小伙伴可以參考一下2022-06-06

