Python—返回文件名中每个唯一客户名称的最新.Tar文件列表
我在“supertar”文件夹中有各种tar文件,标记为:-Python—返回文件名中每个唯一客户名称的最新.Tar文件列表,python,Python,我在“supertar”文件夹中有各种tar文件,标记为:- esarchive--Mona-AB-Test226-8037affd-06d1-4c61-a91f-816ec9cb825f-052222017-4.tar, esarchive--Jackson-HQ-112-ecb5ab6a-c199-402d-9a8a-8c54c8901d66-06092017-4.tar, esarchive--Mona-AB-Test226-8037affd-06d1-4c61-a91f-816ec9cb
esarchive--Mona-AB-Test226-8037affd-06d1-4c61-a91f-816ec9cb825f-052222017-4.tar,
esarchive--Jackson-HQ-112-ecb5ab6a-c199-402d-9a8a-8c54c8901d66-06092017-4.tar,
esarchive--Mona-AB-Test226-8037affd-06d1-4c61-a91f-816ec9cb825f-05202017-4.tar,
esarchive--Jackson-HQ-112-ecb5ab6a-c199-402d-9a8a-8c54c8901d66-06012017-4.tar,
esarchive--Jonah-7fbbbc6c-8463-4ec1-9bde-3fc5429311e5-06092017-4
如何根据每个客户的日期值(文件名末尾)提取其各自文件名中提到的Mona、Jackson、Jonah等客户的最新.tar文件名,以便获得一个具有以下值的变量:
esarchive--Mona-AB-Test226-8037affd-06d1-4c61-a91f-816ec9cb825f-052222017-4.tar,
esarchive--Jackson-HQ-112-ecb5ab6a-c199-402d-9a8a-8c54c8901d66-06092017-4.tar,
esarchive--Jonah-7fbbbc6c-8463-4ec1-9bde-3fc5429311e5-06092017-4
到目前为止,我已经执行了以下代码:-
import sys
import os
import tarfile,sys
import tarfile
import re
names = os.listdir('/home/neel/Desktop/supertar')
def parse_date(name, offset=-10):
try:
date_str = name[offset:offset+8]
return int(date_str[-4:] + date_str[:2] + date_str[2:4])
except (IndexError, TypeError, ValueError): # invalid file name
return -1
sorted_list = [x[1] for x in sorted(((parse_date(l, -14), l) for l in names), reverse=True) if x[0] != -1]
print "The File Being Untarred is:", sorted_list[0]
tar = tarfile.open("/home/neel/Desktop/supertar/"+sorted_list[0])
tar.extractall(path="/home/neel/Documents/tar-dump-es") # untar file here
tar.close()
a=re.match("esarchive--(\w+)-(\w+)-(\w+)", sorted_list[0]).group(1)
b=re.match("esarchive--(\w+)-(\w+)-(\w+)", sorted_list[0]).group(2)
c=re.match("esarchive--(\w+)-(\w+)-(\w+)", sorted_list[0]).group(3)
s = a+'-'+b+'-'+c # s=Mona-AB-Test226,Jackson-HQ-112 etc.
print 'Logging Latest Customer Log For:',s
此代码在排序列表中仅返回任何一个具有最新日期的客户的最新.tar变量。我有许多客户名称,如Mona-AB-Test226等,因此我如何做到这一点,以便从目录中为每个唯一的客户名称(即最新的)获取最新的.tar文件,并以上述代码中提到的格式将其卸载到我的转储文件夹中。首先,在您的文件名中,找到日期
file_date_str = my_file_name.split('-')[-2]
其次,获取datetime对象以进行比较
import datetime
datetime_obj = datetime.datetime.strptime(file_date_str, '%m%d%Y')
进行比较并保存一个文件的最新文件名
name = file_name.split('-')[0].split('--')[1]. # Get Mona, Jackson, ...
try:
(latest_date, _) = my_dict['name']. # _ has file name, which you don't want to compare.
if date > latest_date:
# If entry for this name exists,
# Replace the info with latest date.
my_dict['name'] = (date, file_name)
except KeyError:
# No info for this name in dictionary.
my_dict['name'] = (date, file_name)
在循环中为所有文件运行所有这些代码。最后你会在字典里找到你想要的东西。不要忘记可能需要的初始化和小调试。首先,在文件名中找到日期
file_date_str = my_file_name.split('-')[-2]
其次,获取datetime对象以进行比较
import datetime
datetime_obj = datetime.datetime.strptime(file_date_str, '%m%d%Y')
进行比较并保存一个文件的最新文件名
name = file_name.split('-')[0].split('--')[1]. # Get Mona, Jackson, ...
try:
(latest_date, _) = my_dict['name']. # _ has file name, which you don't want to compare.
if date > latest_date:
# If entry for this name exists,
# Replace the info with latest date.
my_dict['name'] = (date, file_name)
except KeyError:
# No info for this name in dictionary.
my_dict['name'] = (date, file_name)
在循环中为所有文件运行所有这些代码。最后你会在字典里找到你想要的东西。不要忘记可能需要的初始化和小调试