Python Forum

Full Version: Sorting list from FTP by Date
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

i am parsing a ftp f.retrlines('MLSD', ls.append) which creates me a list that contains Fileinfo like the following

#type=file;size=8889;modify=20200719015840; zzapped +2#-lxt.prg

i isolate the info to 3 different lists with the information filesize,modify and filename
that means list a contains "8889" , listb "20200719015840" and listc "zzapped +2#-lxt.prg"

I want the 3 lists to be sorted simultanusly by filedate and create a new list that contains the files sorted by Date.
Or even better, sort the MLSD list by Date while downloading or afterwards .

f = ftplib.FTP()
	f.port = currentport
	f.retrlines('MLSD', ls.append)
Use the builtin sorted() function with its key= keyword argument.
Hi Lastile,
I think you are missing one important detail. You say you have 3 lists. And you want to keep the data together after sorting. This is very difficult.
Instead you should make one list. Each element of this list must also be a list (or tuple or dictionary ...). Each of these nested lists must contain the three elements filesize,modify and filename. So "yourlist[0][0]" would contain the filesize of the first file. And "yourlist[0][2]" the filename etc. Then you can use the sort() or sorted() function with the key= argument as Gribouillis suggested.