Search code examples
pythonsortingstrftime

how to set key arg for sorted() when using strftime to form file path


I am on Windows and trying to find the most recent file in a certain folder. Here is the folder name, C:\ResultsUpload\Nmap. I will have files in this folder resembling the following format C:\ResultsUpload\Nmap\scan-<some hostname>-%Y%m%d%H%M.xml.

Here are two examples, scan-localhost-201808150818.xml and scan-scanme.nmap.org-201808150746.xml

I have the following code,

logdir = r'C:\ResultsUpload\Nmap'  

logfiles = sorted([f for f in os.listdir(logdir) if f.startswith('scan')])
print logfiles

print "Most recent file = %s" % (logfiles[-1],)

Printing logfiles shows as ['scan-localhost-201808150818.xml', 'scan-scanme.nmap.org-201808150746.xml']

Even though the file with localhost as the hostname was more recent, the scanme.nmap.org file is in the [-1] position. I believe this is due to alphabetical sorting. So my sorting is wrong here and I believe I need the sorting key parameter like so

logfiles = sorted([f for f in os.listdir(logdir) if f.startswith('scan')], key= <somethin>)

I'm just not sure how to say that the key is the strftime format or how to adjust the startswith() arg to account for different host names. Would anyone be able to assist?


Solution

  • You can give the key parameter a lambda which will extract the timestamp from the entry.

    By default, the sorting is in natural sorting. You can do a reverse sorting by giving reverse=True

    >>> l= ["scan-localhost-201808150818.xml","scan-scanme.nmap.org-201808150746.xml"]
    >>>
    >>> sorted(l, key = lambda x: x.rsplit('-')[-1].split(".")[0] , reverse = True)
    ['scan-localhost-201808150818.xml', 'scan-scanme.nmap.org-201808150746.xml']
    >>>