2013-02-08 31 views
1

時,如何獲取cmd中的信息我試圖在將文件推送到設備時獲得進度。 當我「設置ADB_TRACE = adb」在CMD(在this page中找到)當我設置ADB_TRACE = adb

它的工作原理然後我想在Python 2.7中使用它。

cmd = "adb push file /mnt/sdcard/file" 
os.putenv('ADB_TRACE', 'adb') 
os.popen(cmd) 
print cmd.read() 

它什麼也沒有顯示。 我怎樣才能獲得這些細節?

操作系統:WIN7

回答

1

os.popen被棄用:

自2.6版本不推薦使用:此功能已經過時了。使用 subprocess模塊。特別檢查替換舊功能 subprocess模塊部分。

使用subprocess代替:

import subprocess as sp 

cmd = ["adb","push","file","/mnt/sdcard/file"] 
mysp = sp.popen(cmd, env={'ADB_TRACE':'adb'}, stdout=sp.PIPE, stderr=sp.PIPE) 
stdout,stderr = mysp.communicate() 

if mysp.returncode != 0: 
    print stderr 
else: 
    print stdout 
+0

感謝,我得到的細節,但該命令簡化版,工作find.I得到了一些「無法創建套接字」錯誤 – tastypear 2013-02-08 19:33:52

+0

你是最歡迎的。既然您已經處理了Python方面的問題,您可能會考慮創建另一個問題,詢問來自'adb'命令的錯誤。 – bernie 2013-02-08 19:52:31

+0

好吧,我將創建另一個:) – tastypear 2013-02-09 08:41:46