2013-07-24 44 views
2

我需要編寫一個腳本,定期從我的Github帳戶下載特定文件。我見過this Github page這似乎是我正在尋找,但我一直無法得到它的工作。也許這是因爲我不太瞭解「用戶」字段是什麼(而不是「用戶名」)。如果有人用這種方法取得了成功,你能舉個例子嗎?另外,如果您使用其他方法,您能否讓我知道?提前致謝!Bash或Python:如何從Github下載單個指定文件?

回答

0

這取決於文件類型。對於非二進制文件,當您在github中打開該文件時,您可以通過單擊「編輯」旁邊的「raw」按鈕來獲取文件的url。然後,您只需使用curlwget即可下載。

這裏是一個圖片,把事情說清楚:

enter image description here

然後複製網址:

enter image description here

+0

這是一個python腳本。我知道我可以查看文件的「原始」,但我不確定兩件事: 1.如何獲取RAW網址? 2.下載時如何處理憑證? – oas8df79as8fysf

+2

我認爲使用'wget url'應該可以。 – jh314

0

假設文件同步,我會寫一個bash腳本,像這樣:

#!/bin/bash 
# file: ./getFromGit 
# cd into git-synced directory and update the file 
cd /directory/path/ 
git checkout origin/branch /path/to/file/in/dirStructure 

然後

$ chmod u+x ./getFromGit 
$ cp getFromGit /usr/local/bin # or wherever your executables like git are 

現在從任何你喜歡的目錄,你可以調用getFromGit,它會讓你的文件 想。

查看this tutorial from Jason Rudolph瞭解更多有關使用git從給定分支中檢出單個文件的信息。

如果它是不同步的,我同意@ jh314:只使用wget

0

這應該工作(未經測試,但應該給你一個想法):

import time 
import subprocess 
import sys 
import shlex 

if __name__ == "__main__": 
    cmd = ("curl -L" if sys.platform == "darwin" else "wget") 
    url = ...... 
    cmd += " {0} -o {1}".format(url, url.split("/")[-1]) 
    p = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 

    (stdout, stderr) = p.communicate() 

    if stderr: 
     raise Exception(stderr) 

    time.sleep(...) 

    # Call another instance of this script 
    subprocess.Popen(['python', sys.argv[0]]) 

它會之後再次調用腳本時間已過,退出。只要命令完成而沒有錯誤,它將繼續再次調用自己。