2

During a running flow, I get a path to a file x.

As a defense mechanism, since it's a part of code that might crash often and rerun itself, after I get this path x, I wish to check this file and see If it was last modified in the last hour.

If it was updated/ changed in the last hour, it means this service is rerunning after a crash and I do not want to call update_file(x, new_data).

If it was updated more than an hour ago, it means I want to override this file with the new data and call update_file(x, new_data).

3
  • 1
    maybe this can help docs.python.org/3/library/os.path.html#os.path.getmtime. But please explain a bit more what you expect and what you've already tried Commented Sep 8, 2019 at 8:58
  • what have you tried and how the results were different from what you expected? Commented Sep 8, 2019 at 9:01
  • I managed to get the time, but I was not successful with comparing that time and now, as @lenik showed in his answer. Thanks. Commented Sep 8, 2019 at 9:06

1 Answer 1

2

This should do the trick:

import os, time

if os.path.getmtime( x ) + 3600 < time.time() :
    update_the_file( ... )
else :
    # don't update
    pass
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.