无本地工作目录的Github远程Repo上的Python更新文件 [英] Python update files on Github remote repo without local working directory

查看:55
本文介绍了无本地工作目录的Github远程Repo上的Python更新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关于推送到无本地工作目录的远程回购(Python push files to Github remote repo without local working directory)问题的后续问题。我想知道,如果该文件已经存在于远程存储库中,而我只想用一个同名的修改后的文件来更新它,该怎么办?(例如,相当于在Github网站上,上传远程上已存在的文件的修改版本)

编辑:我们已提出解决方案:

contents_object = repository.contents(file_path)
push_status = contents_object.update("test_message",contents)
但是,尽管它在一台机器上成功运行,但它在另一台机器上抛出了错误(具体地说,第一行将获得AttributeError)。这是不是因为githorb3的版本可能不同?

推荐答案

似乎很明显,在githeb3版本0.9.6下(此时您将使用pip install github3.py(https://github3py.readthedocs.io/en/master/#installation)),这将起作用(在没有任何本地工作目录的情况下对远程存储库进行更新):

def update_to_git(username,password,path,account,repo,message):
    files_to_upload = [path]
    gh = github3.login(username=username, password=password)
    repository = gh.repository(account, repo)
    for file_info in files_to_upload:
        with open(file_info, 'rb') as fd:
            contents = fd.read()
        contents_object = repository.contents(file_info)
        contents_object.update(message,contents)
但是,如果您使用的是githorb3版本1.0.0a4,则此操作将不起作用。具体地说,您将得到contents_object = repository.contents(file_info)行的AttributeError,这可能是因为githorb3中的实现发生了变化。

这篇关于无本地工作目录的Github远程Repo上的Python更新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆