使用python提取git存储库中文件的作者上次修改的日期

使用python提取git存储库中文件的作者上次修改的日期,python,git,gitpython,Python,Git,Gitpython,好的,我一直在努力从远程git存储库提取数据,并使用Python脚本生成一个csv报告,根据文件最后修改的日期列出文件。我已经能够使用子流程获得最新的代码,并且能够生成报告。这两个函数的代码段如下所示: > import subprocess > process = subprocess.Popen("git pull",stdout=subprocess.PIPE) > output = process.communicate()[0] 用于csv生成 > with

好的,我一直在努力从远程git存储库提取数据,并使用Python脚本生成一个csv报告,根据文件最后修改的日期列出文件。我已经能够使用子流程获得最新的代码,并且能够生成报告。这两个函数的代码段如下所示:

> import subprocess 
> process = subprocess.Popen("git pull",stdout=subprocess.PIPE)
> output = process.communicate()[0]
用于csv生成

> with open('excelout1.csv', 'w') as csv_file:
>     wr = csv.writer(csv_file, delimiter=',')
>     for row in myfilelist:
>         wr.writerow(row)
现在,我得到了所有文件的最后修改日期,但问题是,生成的日期是我本地回购中的文件更新的时间,也就是我进行最新拉取的时间。我想要的是远程存储库中每个文件的最后修改日期和作者

使用Git bash生成上次修改日期的命令是
Git ls files-z | xargs-0-n1-I{}--Git log-1--format=“%ai{}{}{}}sort
。我想知道如何在python脚本中使用此命令。我是python新手,非常感谢您的帮助

编辑:Mufeed建议后使用的当前代码

import os, csv, glob, time
import pandas as pd
import subprocess

process = subprocess.Popen("git pull", stdout=subprocess.PIPE)
output = process.communicate()[0]
p = subprocess.check_output(['git ls-files -z | xargs -0 -n1 -I{} -- git log -1 --format="%ai {}" {} | sort'],cwd = "C:\Users\sherin.sunny\git\ng-ui",shell=True) 
print(p)

print ('-'*60)  # just vanity
date_file_list = []
for dirpath, dirs, files in os.walk(".\src\\"):
    # select the type of file, for instance *.jpg or all files *.*
    for file in glob.glob(dirpath + '/*.component.ts'):

        stats = os.stat(file)

        lastmod_date = time.localtime(stats[8])

        date_file_tuple = lastmod_date, file
        date_file_list.append(date_file_tuple)

#print date_file_list  # test
date_file_list.sort()
date_file_list.reverse()  # newest mod date now first
print ("%-40s %s" % ("filename:", "last modified:"))
myfilelist = []
for file in date_file_list:
    # extract just the filename
    folder, file_name = os.path.split(file[1])
    # convert date tuple to MM/DD/YYYY HH:MM:SS format
    file_date = time.strftime("%m/%d/%y %H:%M:%S", file[0])
    myfilelist.append([file_name, file_date])
with open('excelout1.csv', 'w') as csv_file:
    wr = csv.writer(csv_file, delimiter=',')
    for row in myfilelist:
        wr.writerow(row)

我不知道我是否正确理解了你的问题。检查下面的代码片段。同一子流程模块将输出作为问题描述

import subprocess
p = subprocess.check_output(['git ls-files -z | xargs -0 -n1 -I{} -- git 
log -1 --format="%ai {}" {} | sort'],cwd = "path\to\directory",shell=True) 
#cwd = change working directory   
print(p)
输出

b'2018-06-23 09:42:40 -0700 CONTRIBUTING.md\n
2018-06-23 09:42:40 -0700 data_reader.py\n
2018-06-23 09:42:40 -0700 LICENSE\n
2018-06-23 09:43:37 -0700 README.md\n'

subprocess.check\u输出用于将输出存储到变量中,以便从中提取所需的值。

请参见为什么不使用子流程模块本身?subprocess.check_输出(['git ls files-z | xargs-0-n1-I{}--git log-1--format=“%ai{}{}”{}],shell=True)@mufeed默认情况下不会使用git bash,我不知道我是否理解正确。但是为了得到他想要的结果,我提到的代码就足够了,对吗?我在执行代码后得到了正确的输出。如果我理解错了,请告诉我。谢谢澄清@Peter。我现在明白了。我得到了以下错误:文件名、目录名或卷标语法不正确。subprocess.CalledProcessError:Command'['cd C:/Users/sherin.sunny/git/ng ui/;git ls files-z | xargs-0-n1 I{}--git log-1--format=“%ai{}”{}sort']”返回非零退出状态1。我给出的目录名正确吗?我想在使用windows系统时,在指定路径时需要使用反斜杠。我的代码是在linux中执行的。试试cd C:\Users\sherin.sunny\git\ng uiI将更新后的代码与正向斜杠和反向斜杠一起使用。获取转发的两个错误:文件名、目录名或卷标语法不正确。回溯(最近一次调用上次):subprocess.CalledProcessError:Command'['git ls files-z|xargs-0-n1-I{}--git log-1--format=“%ai{}{}{}{}{}{}{}{sort']”返回非零退出状态1。对于反斜杠:文件“check_fileage.py”,第12行p=subprocess.check{u输出(['git ls files-z}xargs-0-n1 I}--I{}--git log-1--format=“%ai}}| sort'],cwd=“C:\Users\sherin.sunny\git\ng ui”,shell=True)^SyntaxError:(unicode错误)'UnicodeScape'编解码器无法解码位置2-3的字节:截断\uxxxxx转义符是否可以使用双反斜杠进行检查?例如:C:\\Users\\sherin.sunny\\git\\ng用户界面