【问题标题】:Bash or Python: How to download a single specified file from Github?Bash 或 Python:如何从 Github 下载单个指定文件?
【发布时间】:2013-07-24 12:01:13
【问题描述】:

我需要编写一个脚本,定期从我的 Github 帐户下载特定文件。我已经看到this Github page 这似乎是我正在寻找的东西,但我无法让它工作。也许这是因为我不太了解“用户”字段是什么(而不是“用户名”)。如果有人用这种方法取得了任何成功,你能举个例子吗?另外,如果您使用其他方法,请告诉我吗?提前致谢!

【问题讨论】:

    标签: python git bash github download


    【解决方案1】:

    这取决于文件类型。对于非二进制文件,当您在 github 中打开文件时,您可以通过单击“编辑”旁边的“原始”按钮来获取文件的 url。然后,您只需使用curlwget 即可下载。

    这里有一张图让事情更清楚:

    然后复制网址:

    【讨论】:

    • 这是一个python脚本。我知道我可以查看文件的“原始”,但我不确定两件事:1.如何获取原始网址? 2. 下载凭证时如何处理?
    【解决方案2】:

    假设文件已同步,我会编写一个类似这样的 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,它会得到你想要的文件 想要。

    有关使用 git 从给定分支检出单个文件的更多信息,请参阅 this tutorial from Jason Rudolph

    如果没有同步,我同意@jh314:就用wget

    【讨论】:

      【解决方案3】:

      这应该可以工作(未经测试,但应该给你一个想法):

      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]])
      

      时间过后,它会再次调用脚本,然后退出。只要命令完成没有错误,它就会继续再次调用自己。

      【讨论】:

        猜你喜欢
        • 2011-06-04
        • 1970-01-01
        • 2016-01-09
        • 2013-04-16
        • 1970-01-01
        • 2014-02-23
        • 2020-09-11
        • 2017-03-06
        相关资源
        最近更新 更多