Python - pygithub If file exists then update else create
Asked Answered
E

2

1

Im using PyGithub library to update files. Im facing an issue with that. Generally, we have to options. We can create a new file or if the file exists then we can update.

Doc Ref: https://pygithub.readthedocs.io/en/latest/examples/Repository.html#create-a-new-file-in-the-repository

But the problem is, I want to create a new file it if doesn't exist. If exist the use update option.

Is this possible with PyGithub?

Elliot answered 16/8, 2020 at 10:53 Comment(1)
Can't you use this option (pygithub.readthedocs.io/en/latest/examples/…) to see if it exists in the results?Affixation
E
2

I followed The Otterlord's suggestion and I have achieved this. Sharing my code here, it maybe helpful to someone.

from github import Github
g = Github("username", "password")

repo = g.get_user().get_repo(GITHUB_REPO)
all_files = []
contents = repo.get_contents("")
while contents:
    file_content = contents.pop(0)
    if file_content.type == "dir":
        contents.extend(repo.get_contents(file_content.path))
    else:
        file = file_content
        all_files.append(str(file).replace('ContentFile(path="','').replace('")',''))

with open('/tmp/file.txt', 'r') as file:
    content = file.read()

# Upload to github
git_prefix = 'folder1/'
git_file = git_prefix + 'file.txt'
if git_file in all_files:
    contents = repo.get_contents(git_file)
    repo.update_file(contents.path, "committing files", content, contents.sha, branch="master")
    print(git_file + ' UPDATED')
else:
    repo.create_file(git_file, "committing files", content, branch="master")
    print(git_file + ' CREATED')
Elliot answered 17/8, 2020 at 6:19 Comment(0)
T
4

If you are interested in knowing if a single path exists in your repo you can use something like this and then branch your logic out from the result.

from github.Repository import Repository

def does_object_exists_in_branch(repo: Repository, branch: str, object_path: str) -> bool:
    try:
        repo.get_contents(object_path, branch)
        return True
    except github.UnknownObjectException:
        return False

But the method in the approved answer is more efficient if you want to check if multiple files exist in a given path, but I wanted to provide this as an alternative.

Tannatannage answered 8/2, 2023 at 13:52 Comment(0)
E
2

I followed The Otterlord's suggestion and I have achieved this. Sharing my code here, it maybe helpful to someone.

from github import Github
g = Github("username", "password")

repo = g.get_user().get_repo(GITHUB_REPO)
all_files = []
contents = repo.get_contents("")
while contents:
    file_content = contents.pop(0)
    if file_content.type == "dir":
        contents.extend(repo.get_contents(file_content.path))
    else:
        file = file_content
        all_files.append(str(file).replace('ContentFile(path="','').replace('")',''))

with open('/tmp/file.txt', 'r') as file:
    content = file.read()

# Upload to github
git_prefix = 'folder1/'
git_file = git_prefix + 'file.txt'
if git_file in all_files:
    contents = repo.get_contents(git_file)
    repo.update_file(contents.path, "committing files", content, contents.sha, branch="master")
    print(git_file + ' UPDATED')
else:
    repo.create_file(git_file, "committing files", content, branch="master")
    print(git_file + ' CREATED')
Elliot answered 17/8, 2020 at 6:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.