无法使用gitpython库将文件提交到git

无法使用gitpython库将文件提交到git,python,git,gitpython,Python,Git,Gitpython,我有一个本地git存储库。我有一个名为“fibo.py”的python文件。我在编辑器中编辑了该文件并保存了更改。然后我编写了以下代码将该文件提交给repo from git import Repo import os repo_dir = 'D:\\Git Repo\\newrepo1' file_list = [] repo1 = Repo(repo_dir) print(os.getcwd()) os.chdir('%s' % repo_dir) dirs = os.listdir()

我有一个本地git存储库。我有一个名为“fibo.py”的python文件。我在编辑器中编辑了该文件并保存了更改。然后我编写了以下代码将该文件提交给repo

from git import Repo
import os

repo_dir = 'D:\\Git Repo\\newrepo1'
file_list = []
repo1 = Repo(repo_dir)
print(os.getcwd())
os.chdir('%s' % repo_dir)
dirs = os.listdir()
print(os.getcwd())
# This would print all the files and directories
for file in dirs:
    if file != '.git':
        print(file)
        new_file_path = osp.join(repo1.working_tree_dir, file)
        open(file,'r').close
        filelist.append(new_file_path)

commit_message = 'adding new files'
repo1.index.add(file_list)
repo1.index.commit(commit_message)
它正在执行提交。我检查了日志,但在检查提交记录时

print(repo1.untracked_files)
for commit in list(repo1.iter_commits()):
    print('COMMITS--->',commit.stats.files)
它给了我这个输出

['fibo.py']
COMMITS---> {}
COMMITS---> {}
COMMITS---> {}
COMMITS---> {}

除了文件列表和文件列表拼写错误之外,有人能帮我提出修改建议吗。我解决了,只需编辑for循环

for file in dirs:
    if file != '.git':
        print(file)
        new_file_path = osp.join(repo1.working_tree_dir, file)
        print(new_file_path)
        print(repo1.index)
        #open(file,'w').close
        repo1.index.add([new_file_path])

只将repo1.commit放在For循环之后,它就会工作

请修复代码并删除不必要的混乱(打印调试输出等)。您的代码甚至不会运行,原因有多种(
filelist
未定义,没有模块
osp
,…)您的示例为我消除了所有混乱,我得到了
'COMMITS-->',{u'fibo.py':{'deletions':0',line':0',insertions':0}
打印(文件列表)输出什么?