0

I have the following code. I am pulling files from a directory and trying to check if they were created in the past 24hrs but I can't seem to figure out the right time or datetime functions to use to be able to compare the create time against the from and to dates:

def checkdirectory(folder_paths):
    #loop through and check directories for new files
    file_list = []
    for path in folder_paths:
        from_date = datetime.datetime.now() - datetime.timedelta(days=-1)
        to_date = datetime.datetime.now()

        files = glob.glob(path['folder_path'] + path['file_filter'])

        for file in files:
            create_time = os.path.getctime(file)
            print(create_time)
            if datetime.time(create_time) >= from_date and datetime.time(create_time) <= to_date:
               file_list.append(file)
3
  • You want to use datetime.datetime, not datetime.time. And you want to use the fromtimestamp class method to create them from time_t-style seconds-since-1970 timestamps (unless you're on an old version of Python that didn't have that method yet). Commented Aug 30, 2018 at 0:19
  • Just to be clear, you're on Windows, right? Only on Windows does getctime mean "created time"; everywhere else it returns the time of the last metadata update. getmtime is more portable and usually the correct thing to test (since the time of last modification is the time the exact data in the current file first existed). Otherwise, you clearly need to use datetime.datetimes, not datetime.times, because a datetime.time can't represent a concept of "more than 24 hours ago" in the first place. Commented Aug 30, 2018 at 0:20
  • Also, are you expecting to find timestamps in the future? Because otherwise, testing that the creation time is before now is a little pointless; aside from narrow race conditions (files created between to_date = datetime.datetime.now() and the glob.glob call), nothing should be created after now, and in the rare cases it occurs (immediately after a clock sync that moves the clock backwards or the like), you probably still want to treat them as "created in the last 24 hours". Commented Aug 30, 2018 at 0:22

1 Answer 1

1

There are two problems with this:

datetime.time(create_time)

First, you're constructing a time object, which represents a:

time of day, independent of any particular day

In other words, it represents something like "10:30:00.000", not "2018-Aug-29 10:30:00.000". So, that isn't much use if you want to find files that are "newer than yesterday". What you want is a datetime object.


Second, you can't just throw some random value at the constructor and hope it's the right thing.

The getctime function is:

a number giving the number of seconds since the epoch

The time constructor takes:

time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)

And the datetime constructor takes:

datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)

So, you have a number of seconds, and you're trying to use that as an hour or a year.

The function you want to use is fromtimestamp:

Return the local date and time corresponding to the POSIX timestamp


So, what you want is:

datetime.datetime.from_timestamp(create_time)

Also, be aware that what getctime returns is:

the system’s ctime which, on some systems (like Unix) is the time of the last metadata change, and, on others (like Windows), is the creation time

So, using that for something named create_time is at best non-portable and Windows-specific, and at worst (if you're not actually on Windows) wrong.


Also:

from_date = datetime.datetime.now() - datetime.timedelta(days=-1)

You're subtracting -1 days, which is the same as adding 1 day.

So, instead of looking for files less than 24 hours old, you're looking for files more than 24 hours in the future. Either use +, or use days=1.


While we're at it:

if datetime.time(create_time) >= from_date and datetime.time(create_time) <= to_date:

Instead of creating the timestamp object twice and doing two separate comparisons, why not just use a chained comparison?

if from_date <= datetime.datetime.from_timestamp(create_time) <= to_date:

Or, maybe better, move the construction out of the test:

create_time = datetime.datetime.from_timestamp(os.path.getctime(file))
if from_date <= create_time <= to_date:
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.