0

I have code thats upload SQlite3 file to GitHub(module PyGithub).

import github
with open('server.db', 'r') as file:
        content = file.read()
g = github.Github('token')
repo = g.get_user().get_repo("my-repo")
file = repo.get_contents("server.db")
repo.update_file("server.db", "Python Upload", content, file.sha, branch="main")

If you open this file through a text editor, then there will be characters that are not included in UTF-8, since this is a database file. I get this error:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd9 in position 99: invalid continuation byte

How i can fix it?

Maybe I can upload the file to GitHub so it is not text-based, like a PNG?

mooncdx
  • 46
  • 5
  • Do you know what encoding is used for said file? – Daweo Oct 11 '21 at 10:38
  • Maybe ANSI (Windows NotePad Save AS). – mooncdx Oct 11 '21 at 11:04
  • @МаксимПигидин2 definitely not. `ANSI` refers to the 7-bit US-ASCII which is *identical to UTF8*. If you don't specify an encoding, applications will either save using UTF8 or use the encoding specified in your locale settings. Notepad can save as UTF8. If you want to avoid problems, use either UTF8 or UTF16. – Panagiotis Kanavos Oct 11 '21 at 11:27
  • The file is a binary file, and you're opening it in text mode. Open it in `'rb'` mode. – Anon Coward Oct 11 '21 at 19:24

1 Answers1

0

i use this.

f = open("yourtextfile", encoding="utf8")
contents = get_blob_content(repo, branch="main",path_name="yourfile")
repo.delete_file("yourfile", "test title", contents.sha)
repo.create_file("yourfile", "test title", f.read())

and this def

def get_blob_content(repo, branch, path_name):
    # first get the branch reference
    ref = repo.get_git_ref(f'heads/{branch}')
    # then get the tree
    tree = repo.get_git_tree(ref.object.sha, recursive='/' in path_name).tree
    # look for path in tree
    sha = [x.sha for x in tree if x.path == path_name]
    if not sha:
        # well, not found..
        return None
    # we have sha
    return repo.get_git_blob(sha[0])
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 16 '21 at 20:20