This list is organized by day.month.year. Before my list was a prefix. For example:
[Objname].01.02.2020.log. I used a regex (\[Obj.*]).(\d{2}.\d{2}.\d{4}) for splitting the ObjectName from date. This is the resulted:
01.02.2020
02.02.2020
03.02.2020
04.02.2020
05.02.2020
06.02.2020
07.02.2020
08.02.2020
09.02.2020
10.02.2020
11.02.2020
12.02.2020
13.02.2020
14.02.2020
15.02.2020
16.02.2020
17.02.2020
18.02.2020
19.02.2020
20.02.2020
21.02.2020
22.02.2020
23.02.2020
24.02.2020
25.02.2020
26.02.2020
27.02.2020
29.01.2020
30.01.2020
31.01.2020
I used sorted() because the Objectname is composite by numbers and I needed return this files sorted. But I don't know how is the best way to handle with this.
def getFiles(numbers):
currentDay = datetime.datetime.now()
numdays = numbers
dateList = []
for x in range (0, numdays):
date = currentDay - datetime.timedelta(days = x)
days = date.strftime("%d.%m.%Y")
dateList.append(days)
path = "/var/log/"
files = sorted([filename for root, dirs, files in os.walk(path)
for filename in files
for date in dateList
if filename.endswith(date+".log")])
return files