0

The following accesses the data from a text file on git hub.

repoName = 'repo_url'
filePath = 'file.txt'

from github import Github
g = Github(TOKEN)
repo = g.get_repo(repoName)
file = repo.get_contents(filePath)
data = file.decoded_content.decode("utf-8")

I want to add a new line to the data and push that to the file github repo.

data += "\n A new line"

I've been going through the api and googling, but I can't seem to find the best way to do this.

Spatial Digger
  • 1,883
  • 1
  • 19
  • 37

1 Answers1

0

Ok so because i cant do comments Did u try printing the data to see if its actually str bc as i knw u cant add for example int to str and also why didnt you try making a copy of the file.txt (if u dont want to lose the file.txt original data, if u dont care use just file.txt) and use

fileOpen=open("file.txt")
fileData=fileOpen.read()
with open("file.txt", "a+") as toWriteIn:
    toWriteIn.write(f"{fileData}\n A new line")
fileOpen.close()

this adds a new line called A new line

  • I've printed out the data variable, it give the expected string. Ints can be added to strings as long as they are converted first e.g. `str()`. I understand the code you've posted, that is writing to a text file as is normal. As stated in the OP I want to append a new line to a txt file in a github repo. You code could work, but I'd have to pull, unpack the repo, open/write the file and push it back. I simply want to push a new line to a text file already in a github repo. I hope that clarifies my question. – Spatial Digger Jan 05 '23 at 20:32