Reputation: 3
I have scoured many questions and tried many suggestions but I just can't seem to get something simple to work. Essentially I would like to run my script on a loop. If a file is present print and repeat. If file is not present sleep for 19 minutes and then run script again. I am able to get the "if" portion to work fine when a file is present. When no file is present nothing happens. I am also completely lost on making it loop.
import os, shutil
import glob
import time
source = 'C:/File Location/Files'
files = os.listdir(source)
files = glob.iglob(os.path.join(' C:/File Location/Files ', "*.pdf"))
for file in files:
if os.path.isfile(file):
time.sleep(30)
print ("Success")
else:
time.sleep(1140)
Upvotes: 0
Views: 30
Reputation: 3648
The easiest way to do what I think you wish to do is using schedule. You can use this like this:
schedule.every(60*19).seconds.do(<your file function>)
while True:
schedule.run_pending()
time.sleep(1)
This will run your file stuff once every 19 minutes.
If you actually wish to print the file continuously except if it's not present, in which case it sleeps for 19 minutes, you can use:
while True:
if os.path.isfile(file):
time.sleep(30)
print("Succes")
else:
time.sleep(19*60)
Upvotes: 1