[英]how can I silence all of the output from a particular python command?
Autodesk Maya 2012提供了“mayapy” - 一個python的修改版本,其中包含加載Maya文件所需的軟件包,並作為批量工作的無頭3D編輯器。 我是用bash腳本調用的。 如果該腳本使用cmds.file(filepath, open=True)
打開其中的場景文件,它會發出警告,錯誤和其他我不想要的信息的頁面。 我希望把所有這些關僅在cmds.file命令運行。
我已經嘗試從我在shell腳本中發送到mayapy的Python命令內部重定向,但這不起作用。 我可以通過在調用bash腳本時將stdout / err重定向到/ dev / null 來使所有內容靜音。 有沒有辦法在調用shell時使它靜音,但是仍然允許我在腳本中傳入的命令打印出信息?
test.sh:
#!/bin/bash
/usr/autodesk/maya/bin/mayapy -c "
cmds.file('filepath', open=True);
print 'hello'
"
叫它:
$ ./test.sh # spews info, then prints 'hello'
$ ./test.sh > /dev/null 2>&1 # completely silent
基本上,我認為解決此問題的最佳方法是實現一個包裝器,它將執行test.sh並清理輸出到shell。 為了清理輸出,我只是在前面添加一些字符串來通知你的包裝器這個文本適合輸出。 我對包裝文件的靈感來自於: https : //stackoverflow.com/a/4760274/2030274
內容如下:
import subprocess
def runProcess(exe):
p = subprocess.Popen(exe, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while(True):
retcode = p.poll() #returns None while subprocess is running
line = p.stdout.readline()
yield line
if(retcode is not None):
break
for line in runProcess(['./test.sh']):
if line.startswith('GARYFIXLER:'):
print line,
現在你可以想象test.sh就是這樣的東西
#!/bin/bash
/usr/autodesk/maya/bin/mayapy -c "
cmds.file('filepath', open=True);
print 'GARYFIXLER:hello'
"
這只會打印你好的線。 因為我們在子進程中包裝python調用,所以通常顯示給shell的所有輸出都應該被捕獲,你應該攔截你不想要的行。
當然,要從python腳本調用test.sh,您需要確保擁有正確的權限。
我知道我剛剛用管子扭曲了。 Maya確實將所有批量輸出發送到stderror。 一旦你正確地管道stderr,這將完全釋放stdout。 這是一個全效的單行程。
# load file in batch; divert Maya's output to /dev/null
# then print listing of things in file with cmds.ls()
/usr/autodesk/maya/bin/mayapy -c "import maya.standalone;maya.standalone.initialize(name='python');cmds.file('mayafile.ma', open=True);print cmds.ls()" 2>/dev/null
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.