Bash还是Python:如何从Github下载一个指定的单个文件?

4
我需要编写一个脚本,定期从我的Github账户下载特定文件。我看到了this Github page, 这似乎是我要找的东西,但我一直无法让它正常工作。也许这是因为我不太理解"user"字段(而不是"username")的含义。如果有人使用该方法成功,请提供一个例子吗?另外,如果您使用其他方法,请告诉我一下?谢谢!
3个回答

2
取决于文件类型。对于非二进制文件,在GitHub中打开文件后,单击“编辑”旁边的“原始”按钮即可获取文件的URL。然后,您只需使用curlwget进行下载。
下面是一张图片以便更好地理解: enter image description here 然后,复制URL: enter image description here

这是一个Python脚本。我知道我可以查看文件的“原始”版本,但我不确定两件事:
  1. 如何获取RAW URL?
  2. 下载时如何处理凭据?
- oas8df79as8fysf

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函数,它会获取你想要的文件。

请参考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]])

时间到后,它将再次调用脚本并退出。只要命令没有错误完成,它就会不断调用自己。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接