【问题标题】:Can a list of files / file be pushed / committed into GIT using Python?可以使用 Python 将文件/文件列表推送/提交到 GIT 中吗?
【发布时间】:2023-03-25 18:40:01
【问题描述】:

我有下面的代码来列出添加/修改的文件。

   import os
   import datetime as dt

   config_path = '/home/'
   now = dt.datetime.now()
   ago = now-dt.timedelta(minutes=30)

   for root, dirs,files in os.walk(config_path):
       for fname in files:
           path = os.path.join(root, fname)
           st = os.stat(path)
           mtime = dt.datetime.fromtimestamp(st.st_mtime)
           if mtime > ago:
                   print('File : %s Time : %s'%(path, mtime))

我希望使用 Python 将这些文件推送到 GIT。有什么办法吗?

【问题讨论】:

  • 使用python执行bash命令?
  • @evolutionxbox ~ 当然,我可以试一试。

标签: python git python-3.x github


【解决方案1】:

你可以使用 os.system() 或 os.popen() 来调用 git

【讨论】:

    【解决方案2】:

    我更喜欢使用

    subprocess.check_output(['git', 'push'], stderr=subprocess.STDOUT)
    

    因为它会抑制输出,并且仅在发生错误时才抱怨。

    【讨论】:

      【解决方案3】:

      您可以使用GitPython 库来在不离开python 环境的情况下使用git。

      使用该库,您可以与高级和低级 git 存储库进行交互(即,您可以将其用作 git 包装器,或者您可以将文件作为 BLOB 与您的情况进行交互)。

      【讨论】:

        猜你喜欢
        • 2014-02-16
        • 1970-01-01
        • 1970-01-01
        • 2011-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-23
        相关资源
        最近更新 更多