Skip to content Skip to sidebar Skip to footer

Trying To Identify The Newest And Second Newest File In A Directory

I am trying to identify the newest and second newest files in a directory. This is the code I intended to use: CONFIGS = '/Users/root/dev/config-files/' allConfigs = sorted(os.lis

Solution 1:

os.listdir returns relative names, so you'll have to use os.path.join to make them absolute:

allConfigs = sorted(os.listdir(CONFIGS),
    key=lambda p: os.path.getctime(os.path.join(CONFIGS, p))

Solution 2:

I think it is missing closing bracket:

allConfigs = sorted(os.listdir(CONFIGS),
   key=lambda p: os.path.getctime(os.path.join(CONFIGS, p)))

Post a Comment for "Trying To Identify The Newest And Second Newest File In A Directory"