How can I check if a given file is present in a Github repository using PyGithub?

1.2k Views Asked by At

file path = repo_name/abc/xyz.yml

repo (is the git repo object)

I want to be able to check if the repo consists of the file by returning a Boolean.

something like:

def fileInRepo(repo, filePath):
   if filePath in repo.files:
      return true
   else:
      return false
1

There are 1 best solutions below

0
On

I had a problem like yours and I resolved it using this script.

    import re
    import requests
    from bs4 import BeautifulSoup

    def check_script(self, git_repo_link: str, name: str) -> bool:
        try:
            r = requests.get(git_repo_link)
            html_doc = r.text
            soup = BeautifulSoup(html_doc)
            file = soup.find_all(title=re.compile(name))
            if file:
                return True
            else:
                return False
        except Exception:
            return False

Hope this is helpful for you. Thank you.