linux执行python不打印_在Python中执行shell程序而不打印到屏幕
有沒有一種方法可以讓我從Python執行一個shell程序,它將輸出打印到屏幕上,然后將它的輸出讀入變量而不在屏幕上顯示任何內容?在
這聽起來有點令人困惑,所以也許我可以用一個例子來解釋它。在
假設我有一個程序,它在執行時將某些內容打印到屏幕上bash> ./my_prog
bash> "Hello World"
當我想在Python中將輸出讀入變量時,我讀到一個好的方法是使用subprocess模塊,如下所示:
^{pr2}$
有了這個構造,我可以將程序的輸出輸出輸出到my_var(這里是"Hello World"),但是當我運行Python腳本時,它也會被打印到屏幕上。有什么辦法可以抑制這種情況嗎?我在subprocess文檔中找不到任何內容,所以可能還有另一個模塊可以用于此目的?在
編輯:
我剛發現commands.getoutput()讓我這么做。但是在subprocess中是否也有實現類似效果的方法?因為我計劃在某個時候做一個Python的版本。在
編輯2:具體例子
摘自python腳本:oechem_utils_path = "/soft/linux64/openeye/examples/oechem-utilities/"\
"openeye/toolkits/1.7.2.4/redhat-RHEL5-g++4.3-x64/examples/"\
"oechem-utilities/"
rmsd_path = oechem_utils_path + "rmsd"
for file in lMol2:
sReturn = subprocess.check_output("{rmsd_exe} {rmsd_pars}"\
" -in {sIn} -ref {sRef}".format(rmsd_exe=sRmsdExe,\
rmsd_pars=sRmsdPars, sIn=file, sRef=sReference), shell=True)
dRmsds[file] = sReturn
屏幕輸出(請注意,不是“所有內容”都打印到屏幕上,而是
輸出,如果我使用commands.getoutput一切正常:/soft/linux64/openeye/examples/oechem-utilities/openeye/toolkits/1.7.2.4/redhat-RHEL5-g++4.3-x64/examples/oechem-utilities/rmsd: mols in: 1 out: 0
/soft/linux64/openeye/examples/oechem-utilities/openeye/toolkits/1.7.2.4/redhat-RHEL5-g++4.3-x64/examples/oechem-utilities/rmsd: confs in: 1 out: 0
/soft/linux64/openeye/examples/oechem-utilities/openeye/toolkits/1.7.2.4/redhat-RHEL5-g++4.3-x64/examples/oechem-utilities/rmsd - RMSD utility [OEChem 1.7.2]
/soft/linux64/openeye/examples/oechem-utilities/openeye/toolkits/1.7.2.4/redhat-RHEL5-g++4.3-x64/examples/oechem-utilities/rmsd: mols in: 1 out: 0
/soft/linux64/openeye/examples/oechem-utilities/openeye/toolkits/1.7.2.4/redhat-RHEL5-g++4.3-x64/examples/oechem-utilities/rmsd: confs in: 1 out: 0
總結
以上是生活随笔為你收集整理的linux执行python不打印_在Python中执行shell程序而不打印到屏幕的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 常用来进行钢结构节点输出的软件是什么_R
- 下一篇: linux 其他常用命令