Home > Back-end >  Trying to get oldest file in entire directory
Trying to get oldest file in entire directory

Time:09-01

I am trying to get the oldest file in the entire directory, but I keep getting the youngest. Is there something I am missing?

#gets oldest file
def get_oldest(value):
    for root, dirs, files in os.walk(value):
        current_it = os.path.getmtime(root)
        oldest_time = time.time()
        if current_it < oldest_time:
            oldest_time = current_it
    time_val = datetime.utcfromtimestamp(oldest_time).strftime('%Y-%m-%d')
    return time_val

#gets youngest file
def get_youngest(value):
    for root, dirs, files in os.walk(value):
        current_it = os.path.getmtime(root)
        if current_it > 0:
            output = current_it
    time_val = datetime.utcfromtimestamp(output).strftime('%Y-%m-%d')
    return time_val

CodePudding user response:

Thank you Marco! That made me realize that I had to define "oldest_time" before the for loop. It is working now!

CodePudding user response:

def get_oldest(value):
    for root, dirs, files in os.walk(value):
        # this is modification time. you might have actually wanted
        # get ctime
        current_it = os.path.getmtime(root) 
        # this is now
        oldest_time = time.time()
        # this checks if the last modification time was before now,
        # which will almost always be true
        if current_it < oldest_time:
             # so this almost always happens
            oldest_time = current_it
    time_val = datetime.utcfromtimestamp(oldest_time).strftime('%Y-%m-%d')
    return time_val

Since you are resetting oldest_time in each run of the loop, the last file visited by walk will be recorded as the "oldest", which is obviously not what you want. It's also worth noting that if there are no files at value, oldest_time will never be set, so time_val = ... oldest_time ... will throw an error.

I think it's likely that you meant to say oldest_time = time.time() above the loop. That way, it is always set even if there are no files to be walked, and your comparison if current_it < oldest_time: does what you probably intended.

def get_youngest(value):
    for root, dirs, files in os.walk(value):
        current_it = os.path.getmtime(root)
        # getmtime returns the number of seconds since
        # 0:00:00 UTC on 1 January 1970, so it will always be
        # larger than 0 unless your system clock is very wrong
        if current_it > 0:
            # as such, this will happen on every iteration of the loop
            output = current_it
    time_val = datetime.utcfromtimestamp(output).strftime('%Y-%m-%d')
    return time_val

Again, your output will simply be the last file visited, not the youngest. What you need to do is similar to what you needed to do with get_oldest. First, set output to 0 above the loop. Then, change your condition to if current_it > output:.

  • Related