2017-03-01 49 views
0

我正在使用gitpython來做一些文件處理。一切工作正常,除了d.new_file條件,我遇到這種類型的錯誤,我不能連接NoneType和str在一起。TypeError:不支持的操作數類型爲+:'NoneType'和'str'使用GITPYTHON

我認爲d.a_path是無,但我不明白爲什麼它不工作,但它在其他條件下工作。

for d in repo.head.commit.diff(None,create_patch=True): 
    if d.deleted_file: 
     print("deleted file") 

    elif d.new_file: 
     print(" new_file") 

     with open(main_dir,'a+') as main_file: 
      main_file.write(d.a_path +'\n') 

    elif d.renamed_file: 
     print("renamed file") 

    else: 
     print(" modified file") 

     with open(main_dir,'a') as main_file: 
      main_file.write(d.a_path+"\n") 

     handle_diff(d.diff) 

有人知道爲什麼d.a_path爲None爲d.new_file條件?

回答

1

這是documented behaviour

There are a few cases where None has to be expected as member variable value:

New File:

a_mode is None 
a_blob is None 
a_path is None

Deleted File:

b_mode is None 
b_blob is None 
b_path is None

新文件的路徑應該是b_path一個新的文件,而不是在a_path,因爲文件並未a存在。

相關問題