[英]How to capture the output of git clone command running from python subprocess
[英]Python How to Capture All Output from Command Line Calls
我正在嘗試從python腳本檢查系統是否安裝了蟒蛇。
我對如何執行此操作的想法是從python腳本調用“哪個conda”並獲取輸出。
盡管出於某種原因,看來這是不可能的。 在我的計算機(運行High Sierra的2015 Macbook Pro)上,似乎調用此方法會引發某種錯誤。 沒問題,只是python在任何情況下似乎都無法捕獲調用的錯誤輸出。
我已經嘗試過subprocess.call,os.system,subprocess.run,subprocess.check_call,subprocess.check_output。
在所有這些情況下,盡管聲稱可以使用stderr = subprocess.PIPE或stderr = subprocess.STDOUT將錯誤輸出轉發至stdout,但我仍然無法從這些調用中獲得任何信息。
我在命令行中看到,當我調用“哪個conda”時,它會輸出大量文本。
在python中,即使當我按照指示捕獲錯誤時,輸出也是一個空字符串。
這令人難以置信,因為這樣做似乎非常簡單。 我現在花了很多時間 可以在命令行上完成。 為什么不能用這種高級語言來完成?
任何幫助/提示,不勝感激。 謝謝!
我沒有蟒蛇...我只是換成了python:
>>> from os import popen
>>> cmd = "which python"
>>> for i in popen(cmd):
... print(i)
...
/usr/bin/python
您還可以執行以下操作來獲取路徑的最后一部分:
from os import path, popen
cmd = "which python"
>>> for i in popen(cmd):
... print(path.basename(i))
...
python
這也為我工作:
>>> for i in popen('whereis python'):
... for j in i.split():
... print(j)
...
python:
/usr/bin/python2.7-config
/usr/bin/python3.5
/usr/bin/python3.5m
/usr/bin/python
/usr/bin/python2.7
/usr/bin/python3.5m-config
/usr/bin/python3.5-config
/usr/lib/python3.5
/usr/lib/python2.7
/etc/python3.5
/etc/python
/etc/python2.7
/usr/local/lib/python3.5
/usr/local/lib/python2.7
/usr/include/python3.5
/usr/include/python3.5m
/usr/include/python2.7
/usr/share/python
/usr/share/man/man1/python.1.gz
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.