Python: Find and print duplicate files based on timestamp in name

Viewed 39

So, I have this list of files:

input_list = ["folder/1611235550000_chicken.json", "folder/1611235723000_dog.json", "folder/1622235723000_dog.json"]

And I need to find the duplicated animal names and, from there, print the oldest one based on the epoch_timestamp (the one with the smallest timestamp).

The file names always follow the same template: "folder/<epoch_timestamp>_<animal_name>.json". For the list above, it should print:

"folder/1611235723000_dog.json"

This is what I have so far:

def duplicate(input_list):
    return list(set([x for x in input_list if input_list.count(x) > 1]))

if __name__ == '__main__':
    input_list = ["folder/1611235550000_chicken.json", "folder/1611235723000_dog.json", "folder/1622235723000_dog.json"]
    print(duplicate(input_list))

But this does not return anything because the epoch_timestamps are different.

1 Answers

This code assumes that the animal names do not have characters like /, ., or _.

l = ["folder/1611235550000_chicken.json", "folder/1611235723000_dog.json", "folder/1622235723000_dog.json"]

d = {} # holds animals:timestamps

for i in range(len(l)):

    for c in range(len(l[i])): # finding split points to make it easier
        if l[i][c] == "/":
            slash = c
        elif l[i][c] == "_":
            u_score = c
        elif l[i][c] == ".":
            dot = c
            break
    
    # converting it to a dictionary; use int() to use min() later
    animal = l[i][u_score+1:dot]
    time = int(l[i][slash+1:u_score])

    # use d.get() so it won't raise a KeyError
    d[animal] = d.get(animal, []) + [time] # add the timestamp to the list

for animal in d: # go through keys/animals
    if len(d[animal]) > 1: # if there is a duplicate
        print("folder/" + str(min(d[animal])) + "_" + animal + ".json")
Related