Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I check file modified in python as a boolean, so I can implement in IF Statement?

Im trying to create a function where if file modified, then perform task. I am able to do this if a file exists by doing –

import os
file_path = 'C:/Hi/'
if os.listdir(file_path):
  do xyz

But how can i do xyz based on if a files date was modified?

os.path.getmtime(file_path)

Only gets the time but cant be used in my if statement

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try:

import os 
import time
no_of_seconds_since_last_check = 30
if time.time() - os.path.getmtime(file_path) < no_of_seconds_since_last_check : 
    print("doing xyz")

The if condition detects a file modification within the specified time span, preferably since the last check for modifications.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading