Я хочу удалить несколько файлов из определенного каталога на основе имени файла, даты и номера версии, используя Python. PS Дата создания файла не может быть принята во внимание
Ссылка опубликована в Stackoverflow, но имена файлов и номера версий изменены. Как сохранить дату и номер версии, чтобы найти последний файл.
source = r'C:\Users\XMLFiles'
file_names = os.listdir(source)
latest_files = {}
for file_name in file_names:
name_parts = file_name.split("_")
date_stamp = name_parts[2], name_parts[3].split(".")[0]
if date_stamp not in latest_files or file_name > latest_files[date_stamp]:
latest_files[date_stamp] = file_name
print(latest_files)
keep_files = latest_files.values()
for file_name in file_names:
if file_name in keep_files:
continue
os.remove(os.path.join(source, file_name)
##################
List of files to process
=============================
Invoice_456879_20180404_2510.xml
Invoice_123876_20171027_17.xml
Invoice_123876_20180404_2513.xml
Invoice_832765_20170309_2.xml
Invoice_832765_20170313_0.xml
Invoice_832765_20170323_5.xml
Invoice_832765_20170330_2.xml
Invoice_832765_20170613_3.xml
Invoice_832765_20171206_18.xml
Invoice_832765_20171206_30.xml
Invoice_832765_20171206_36.xml
Invoice_832765_20180404_3066.xml
Invoice_832765_20180405_9770.xml
Invoice_832765_20180405_9779.xml
Invoice_698325_20170308_0.xml
Invoice_698325_20170309_3.xml
Invoice_698325_20170323_4.xml
Invoice_698325_20170330_5.xml
Invoice_698325_20170613_4.xml
Invoice_698325_20171206_8.xml
Invoice_698325_20171206_24.xml
Invoice_698325_20171206_46.xml
Invoice_698325_20180404_3067.xml
Invoice_698325_20180405_9771.xml
===========================================================
Expected Output
Invoice_456879_20180404_2510.xml
Invoice_123876_20180404_2513.xml
Invoice_832765_20180405_9779.xml
Invoice_698325_20180405_9771.xml