首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PyGithub -如何获取存储库中子文件夹的内容

PyGithub -如何获取存储库中子文件夹的内容
EN

Stack Overflow用户
提问于 2018-11-29 04:43:43
回答 3查看 1K关注 0票数 1

我正在尝试获取给定github存储库中"github“子文件夹的内容。这似乎不起作用。

代码语言:javascript
复制
repo = g.get_repo("PyGithub/PyGithub")
contents = repo.get_contents("github")
while len(contents) > 1:
    file_content = contents.pop(0)
    if file_content.type == "dir":
        contents.extend(repo.get_contents(file_content.path))
    else:
        print(file_content)
EN

回答 3

Stack Overflow用户

发布于 2019-11-05 17:24:31

代码片段工作得很好:

你得到了什么错误?

票数 1
EN

Stack Overflow用户

发布于 2020-08-31 02:59:18

对上面的答案做了一些小的修正。使用len(contents)>0,,否则会丢失一个文件

代码语言:javascript
复制
from github import Github
g = Github()
repo = g.get_repo("PyGithub/PyGithub")
contents = repo.get_contents("github")
while len(contents)>0:
  file_content = contents.pop(0)
  if file_content.type=='dir':
    contents.extend(repo.get_contents(file_content.path))
  else :
    print(file_content)
票数 0
EN

Stack Overflow用户

发布于 2021-07-24 21:12:41

代码语言:javascript
复制
from github import Github

access_token = "[access_tokey]"

hub = Github(access_token) # Github(user, pass)

# Displaying all the existing Repositories and files
for repo in hub.get_user().get_repos():
    # Displaying repo name
    print("Repository [{}]".format(repo.name))
    print("_" * 50)
    # Displaying Contents
    print("[Contents]")
    count = 1
    for content in repo.get_contents(""):
        print("{}. {} [{}]".format(count, content.path, content.type))
        if content.type == 'dir':
            new_count = 1
            # Displaying contents of sub-director
            for sub_content in repo.get_contents(content.path):
                print("    {}. {} [{}]".format(new_count, sub_content.path, sub_content.type))
                new_count += 1
                # Displaying contents of sub directory of sub directory
                if sub_content.type == 'dir':
                    another_count = 1
                    for datain in repo.get_contents(sub_content.path):
                        print("        {}. {} [{}]".format(another_count, datain.path, datain.type))
                        another_count += 1

        count += 1
    print("-" * 50)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53527783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档