2013-12-16 64 views
0

我想在Python中調用「git clone」命令。我希望Python腳本可以將GIT命令輸出顯示在屏幕上,就像在終端中運行一樣。例如克隆進程的百分比信息。無論如何,它是用Python來做的嗎?如何在Python中「實時」顯示命令輸出到屏幕?

+0

感謝。我已經嘗試了該帖子中提到的解決方案。但它仍然沒有像我期望的那樣行事。
例如:
我只能捕捉1行
>>> Cloning into 'suba' ...
但是終端的輸出應爲:
Cloning into 'suba'... remote: Counting objects: 6, done. remote: Compressing objects: 100% (3/3), done. remote: Total 6 (delta 0), reused 0 (delta 0) Receiving objects: 100% (6/6), done. Checking connectivity... done. enchanter

回答

0

看看Python的子進程模塊,你可以捕獲輸出到一個變量,然後使用它。有攔截方向和粗壯的方法。它是從2.4開始提供的,應該可以做到。我已經將這個用於運行系統命令的腳本來捕獲工作中的輸出。回覆如果您需要一個例子,我可以挖掘從我明天早上的工作電腦一升起來......

http://docs.python.org/2/library/subprocess.html

try: 
     # Needs to all be one argument for acme command... 
     cmd = ["acme nw -proj " + self.based_on] 
     p = subprocess.Popen(cmd, 
          cwd=place, 
          shell=True, 
          stdout=subprocess.PIPE, 
          stderr=subprocess.PIPE) 
     output, error_msgs = p.communicate() 
    except (OSError) as err: 
     sys.stdout.write("ERROR {0} ({1})\n".format(err.strerror, 
                err.errno)) 
     sys.exit(err.errno) 
    if len(error_msgs) > 0 and error_msgs != "Scanning the source base and preparing the workspace, please wait ...\n": 
     sys.stdout.write("There were errors, during view create\n") 
     sys.stdout.write(error_msgs) 
    else: 
     sys.stdout.write("SUCCESS\n") 
+0

我已閱讀文檔。並在重複的問題中嘗試解決方案。但沒有任何工作正確。我只能得到一行git clone命令輸出。 git命令輸出中缺少所有統計信息。 – enchanter

+0

也許你可以用代碼更新文章? – pcm

相關問題