![](/img/trans.png)
[英]Getting the last modified date of folders and files with Python and ftplib
[英]Extracting last modified date, author of files in git repository using python
好的,我一直在努力從遠程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)
所以現在,我正在獲取所有文件的最后修改日期,但事實是,生成的日期顯然是本地存儲庫中文件的更新日期,即,當我進行最新的提取時。 我想要的是REMOTE存儲庫中每個文件的最后修改日期和作者。
使用Git bash生成最后修改日期的命令是git ls-files -z | xargs -0 -n1 -I{} -- git log -1 --format="%ai {}" {} | sort
git ls-files -z | xargs -0 -n1 -I{} -- git log -1 --format="%ai {}" {} | sort
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_output用於將輸出存儲到變量,以便您可以從中提取所需的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.