Ok, that is VERY helpful.
I improved on your code so that I remove extension at the end, and then get datetime string from both date and time.
I do have problem that I need to sort items in a list according to first column, and only then based on date.
Here is latest code:
Ok, I resolved sorting with:
I improved on your code so that I remove extension at the end, and then get datetime string from both date and time.
I do have problem that I need to sort items in a list according to first column, and only then based on date.
Here is latest code:
# Is there a way to modify this to only give me only the newest MP3 file? import os from os import listdir from os.path import isfile, join from datetime import datetime from pprint import pprint def removeext(path): return os.path.splitext(path)[0] MyDir = 'c:/test' os.chdir(MyDir) pst_files = [] ista2 = [] #lista = [f for f in listdir(MyDir) if isfile(join(MyDir, f))] lista = ['archive.pst 2016-10-14 080101.pst', 'archive.pst 2016-10-15 080101.pst', 'archive.pst 2016-10-17 080101.pst', 'archive.pst 2016-10-18 080101.pst', 'archive.pst 2016-10-19 080101.pst', 'archive.pst 2016-10-20 080101.pst', 'Outlook.pst 2016-10-14 080101.pst', 'Outlook.pst 2016-10-15 080101.pst', 'Outlook.pst 2016-10-17 080101.pst', 'Outlook.pst 2016-10-18 080101.pst', 'Outlook.pst 2016-10-19 080101.pst', 'Outlook.pst 2016-10-20 080101.pst', 'Outlook.sharing.xml.obi 2016-10-14 080101.obi', 'Outlook.sharing.xml.obi 2016-10-15 080101.obi', 'Outlook.sharing.xml.obi 2016-10-17 080101.obi', 'Outlook.sharing.xml.obi 2016-10-18 080101.obi', 'Outlook.sharing.xml.obi 2016-10-19 080101.obi', 'Outlook.sharing.xml.obi 2016-10-20 080101.obi'] print("lista:") print(lista) for line in lista: line = os.path.splitext(line)[0] lista2.append(line.split()) print("lista2:") pprint(lista2) print("lista2 sorted:") pprint(sorted(lista2, key=lambda x: datetime.strptime(x[1] + " " + x[2], "%Y-%m-%d %H%M%S"), reverse=True))
Output:lista:
['archive.pst 2016-10-14 080101.pst', 'archive.pst 2016-10-15 080101.pst', 'archive.pst 2016-10-17 080101.pst', 'archive.pst 2016-10-18 080101.pst', 'archive.pst 2016-10-19 080101.pst', 'archive.pst 2016-10-20 080101.pst', 'Outlook.pst 2016-10-14 080101.pst', 'Outlook.pst 2016-10-15 080101.pst', 'Outlook.pst 2016-10-17 080101.pst', 'Outlook.pst 2016-10-18 080101.pst', 'Outlook.pst 2016-10-19 080101.pst', 'Outlook.pst 2016-10-20 080101.pst', 'Outlook.sharing.xml.obi 2016-10-14 080101.obi', 'Outlook.sharing.xml.obi 2016-10-15 080101.obi', 'Outlook.sharing.xml.obi 2016-10-17 080101.obi', 'Outlook.sharing.xml.obi 2016-10-18 080101.obi', 'Outlook.sharing.xml.obi 2016-10-19 080101.obi', 'Outlook.sharing.xml.obi 2016-10-20 080101.obi']
lista2:
[['archive.pst', '2016-10-14', '080101'],
['archive.pst', '2016-10-15', '080101'],
['archive.pst', '2016-10-17', '080101'],
['archive.pst', '2016-10-18', '080101'],
['archive.pst', '2016-10-19', '080101'],
['archive.pst', '2016-10-20', '080101'],
['Outlook.pst', '2016-10-14', '080101'],
['Outlook.pst', '2016-10-15', '080101'],
['Outlook.pst', '2016-10-17', '080101'],
['Outlook.pst', '2016-10-18', '080101'],
['Outlook.pst', '2016-10-19', '080101'],
['Outlook.pst', '2016-10-20', '080101'],
['Outlook.sharing.xml.obi', '2016-10-14', '080101'],
['Outlook.sharing.xml.obi', '2016-10-15', '080101'],
['Outlook.sharing.xml.obi', '2016-10-17', '080101'],
['Outlook.sharing.xml.obi', '2016-10-18', '080101'],
['Outlook.sharing.xml.obi', '2016-10-19', '080101'],
['Outlook.sharing.xml.obi', '2016-10-20', '080101']]
lista2 sorted:
[['archive.pst', '2016-10-20', '080101'],
['Outlook.pst', '2016-10-20', '080101'],
['Outlook.sharing.xml.obi', '2016-10-20', '080101'],
['archive.pst', '2016-10-19', '080101'],
['Outlook.pst', '2016-10-19', '080101'],
['Outlook.sharing.xml.obi', '2016-10-19', '080101'],
['archive.pst', '2016-10-18', '080101'],
['Outlook.pst', '2016-10-18', '080101'],
['Outlook.sharing.xml.obi', '2016-10-18', '080101'],
['archive.pst', '2016-10-17', '080101'],
['Outlook.pst', '2016-10-17', '080101'],
['Outlook.sharing.xml.obi', '2016-10-17', '080101'],
['archive.pst', '2016-10-15', '080101'],
['Outlook.pst', '2016-10-15', '080101'],
['Outlook.sharing.xml.obi', '2016-10-15', '080101'],
['archive.pst', '2016-10-14', '080101'],
['Outlook.pst', '2016-10-14', '080101'],
['Outlook.sharing.xml.obi', '2016-10-14', '080101']]
Process finished with exit code 0
Ok, I resolved sorting with:
pprint(sorted(lista2, key=lambda x: (x[0], datetime.strptime(x[1] + " " + x[2], "%Y-%m-%d %H%M%S")), reverse=True))