Python Forum
How to create def for sorted() from list of versioning files (filename+datetime)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create def for sorted() from list of versioning files (filename+datetime)
#5
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:

# 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))
Reply


Messages In This Thread
RE: How to create def for sorted() from list of versioning files (filename+datetime) - by DrLove73 - Jan-12-2017, 10:38 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete strings from a list to create a new only number list Dvdscot 8 1,586 May-01-2023, 09:06 PM
Last Post: deanhystad
  list the files using query in python arjunaram 0 687 Mar-28-2023, 02:39 PM
Last Post: arjunaram
  Create new folders and copy files cocobolli 3 1,525 Mar-22-2023, 10:23 AM
Last Post: Gribouillis
  Failing to print sorted files tester_V 4 1,308 Nov-12-2022, 06:49 PM
Last Post: tester_V
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,568 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  How to download a list of files from FTP? schnarkle 0 1,026 Jun-21-2022, 10:35 PM
Last Post: schnarkle
  Rename part of filename in multiple files atomxkai 7 7,422 Feb-18-2022, 10:03 PM
Last Post: atomxkai
  Python code to read second line from CSV files and create a master CSV file sh1704 1 2,430 Feb-13-2022, 07:13 PM
Last Post: menator01
  Append files and add column with last part of each filename NiKirk 0 2,643 Feb-04-2022, 07:35 AM
Last Post: NiKirk
  set and sorted, not working how expected! wtr 2 1,302 Jan-07-2022, 04:53 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020