【问题标题】:Is there way to write to a github text file with python? [duplicate]有没有办法用 python 写入 github 文本文件? [复制]
【发布时间】:2019-02-11 20:33:31
【问题描述】:

我正在尝试从 Github 存储库中读取文本文件,然后将新内容写入其中,我设法读取部分代码来工作,但显然普通的 file.write() 不适用于文本文件在 github 存储库中。那么有没有办法以某种方式更新文本文件?

filepath = 'file.txt'
with open(filepath) as fp:
    line = fp.readline()
    print(line)
      #fp.write("This won't work, I know")

【问题讨论】:

  • 您指的是本地 github 中您要更改、提交和推送的文件吗?
  • 你必须以写模式打开文件,因为 with open(filename, 'w') as fp
  • 这不是已回答问题的重复。 OP 似乎正在写入本地存储库中的文件。他们可能只需要以写入模式打开文件处理程序。或者发布他们的回溯,以便我们更好地提供帮助。

标签: python python-3.x github


【解决方案1】:

你以读取模式打开文件,这是python的默认模式, 所以:

with open(filepath) as fp:

等价于

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

意味着你以读取模式打开它,使用附加模式写入它

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

github文件没有什么特别之处,错误在你的python代码中

【讨论】:

    猜你喜欢
    • 2020-05-21
    • 1970-01-01
    • 2019-07-02
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 2017-08-01
    相关资源
    最近更新 更多