PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 12, 2022

[FIXED] How to append a file using Pygithub

 May 12, 2022     append, file, github, python, write     No comments   

Issue

I want to write to a already existing .txt file using PyGithub package in python. I have tried this code

git = Github(TOKEN)
repo = git.get_repo("Repository")
file = repo.get_contents("Textfile.txt", ref="Ref")

repo.update_file(file.path, "test", "Text I wanna store", file.sha, branch="Ref")

but this code deletes the old data and only store this data i.e. "Text I wanna store" only

I wanna store this data in continuation with previously store date like "Previous Data Text I wanna store"

and sorry for bad english


Solution

Your code is above is already good, you simply need to take the original Content of the File, and append new one with a + operator or an f-string

Here is an example

git = Github(TOKEN)
repo = git.get_repo("Repository")
file = repo.get_contents("Textfile.txt", ref="Ref")
new_data = input("Text you want to add")
update_file(file.path, "NEW COMMIT", f"{file} {new_data}", file.sha,branch="Ref")


Answered By - urwolfiii
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing