0

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)    

4
  • What do you expect to "happen" when the program goes to sleep for 19 minutes? Commented Mar 9, 2019 at 15:14
  • Should the program stop if it finds the file, or just keep printing it as fast as possible? Commented Mar 9, 2019 at 15:17
  • So I set the sleep time for 30 seconds instead of 19 minutes to see if it did in fact sleep and it did not. When the time.sleep(30) runs during the if portion of the statement my mouse curser shows that a process is happening. Then it prints as it should. The else portion nothing happens, no mouse cursor, nothing. Commented Mar 9, 2019 at 15:20
  • If it finds a file it should print once. I will then move file to another folder (additional code I didn't include because it is not an issue). Then I want it to run again in case there are multiple files. If no additional files sleep for 19 minutes and then run again. Commented Mar 9, 2019 at 15:22

1 Answer 1

1

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)
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.