Nov-25-2018, 10:39 PM
Hi everyone.
I wrote a little script to find the most recently-modified folder inside a particular folder (on Mac OS 10.14)
Most of the code below is all my own own work, but I couldn't figure out how to get the dictionary key associated with the highest value. I went Googling, and found a solution which worked, hoping to reverse-engineer it so I'd understand - but it's baffling me. There's no question the code DOES work, but I don't understand why or how.
The relevant line in the code below is:
The full code:
Amongst the things I don't understand with this code is why key=modified_times.get doesn't have parentheses after the key method - the examples I've found online for dict.get() all do. I also don't understand why max() works, because the supplied parameter is a dictionary, not an iterable, and I haven't specified keys or values to iterate through.
I hate using code I don't understand, so any comments would be very welcome - I'd like to figure this out.
Thanks and kind regards,
Rich
I wrote a little script to find the most recently-modified folder inside a particular folder (on Mac OS 10.14)
Most of the code below is all my own own work, but I couldn't figure out how to get the dictionary key associated with the highest value. I went Googling, and found a solution which worked, hoping to reverse-engineer it so I'd understand - but it's baffling me. There's no question the code DOES work, but I don't understand why or how.
The relevant line in the code below is:
newest_folder = max(modified_times, key=modified_times.get)I've read the docs for both max() and dict.get() and I'm just not (heh) getting it. Could some kind soul explain in words of one syllable what's going on, please?

The full code:
import os # This script populates a dict with {sub-folder:time it was last modified} for all sub-folders in a given folder. # It then finds the sub-folder with the most recent modified time target_path = '/Volumes/Particular-path-of-interest/' # Actual path has been redacted for posting os.chdir(target_path) folders = os.listdir('.') modified_times = {} for folder in folders: if os.path.isdir(folder): # MacOS puts hidden files in folders which can affect results if not filtered out modified_times[folder] = os.path.getmtime(folder) # All the code above I'm fine with... newest_folder = max(modified_times, key=modified_times.get) # This is the line I don't understand print(newest_folder)newest_folder returns a correctly returns a string value which is indeed the name of the most-recently modified folder.
Amongst the things I don't understand with this code is why key=modified_times.get doesn't have parentheses after the key method - the examples I've found online for dict.get() all do. I also don't understand why max() works, because the supplied parameter is a dictionary, not an iterable, and I haven't specified keys or values to iterate through.
I hate using code I don't understand, so any comments would be very welcome - I'd like to figure this out.
Thanks and kind regards,
Rich