0

I am trying to read text file from Github repository and then write new stuff into it, I managed to get reading part of the code to work, but obviously the normal file.write() wouldn't work on text file that's in the github repository. So is there way to somehow update text file?

filepath = 'file.txt'
with open(filepath) as fp:
    line = fp.readline()
    print(line)
      #fp.write("This won't work, I know")
Walnut
  • 51
  • 1
  • 7
  • 3
    Are you referring to a file that is in a local github that you want to change, commit and push? – FlyingTeller Sep 06 '18 at 14:31
  • Use the [GitHub API to push updated file contents to a repository](https://developer.github.com/v3/repos/contents/#update-a-file). See the duplicate. – Martijn Pieters Sep 06 '18 at 14:37
  • you must be opening the file in write mode syntax for that is **with open( _filename_, 'w') as fp**. – Underoos Sep 06 '18 at 14:40
  • 1
    This is not a duplicate of the question that was answered. OP seems to be writing to a file in a local repository. They may just need to open the file handler in write mode. Or post their traceback so we can better assist. – Jordan Epstein Sep 06 '18 at 14:57

1 Answers1

1

you open the file in read mode, which is the default in python, so:

with open(filepath) as fp:

is equivalent to

with open(filepath, 'r') as fp:

Meaning you open it in read mode,use append mode to write to it

with open(filepath, 'a') as fp:

There is nothing special about github files, the error is in your python code

E.Serra
  • 1,495
  • 11
  • 14