python script to poll a folder continuously after a certain time interval and copy the new files to another location

StackOverflow https://stackoverflow.com/questions/17818632

  •  04-06-2022
  •  | 
  •  

Question

I want to poll a folder continuously for any new files, lets say every 1 hours and whenever it finds a new file, it copies to a specific location. I found code to find latest file and to copy to another location. How do I merge this two to get the above desired result? This also may be helpful How to get the most recent file

Was it helpful?

Solution

For polling, the simplest solution is time.sleep(n) which sleeps for n seconds. Your code would look something like this, then:

import time.sleep as sleep
import sys

try:
    while True:
        # code to find the latest file

        # code to copy it to another location

        sleep(3600)
except KeyboardInterrupt:
    print("Quitting the program.")
except:
    print("Unexpected error: "+sys.exc_info()[0])
    raise

(Because this loop can run forever, you should definitely wrap it in a try/except block to catch keyboard interrupts and other errors.) Cron jobs are a perfectly good option if you're only going to be on *nix platforms, of course, but this provides platform independence.

OTHER TIPS

The periodic nature of it suggests that you can use a cron job for it. You can set a cron job to run your python script every hour. It's then the script that handles copying of the file. That is if you're on a Unix machine

crontab -e // this will open your crontab file, then add
0 * * * * /path/to/your/script.py

above will run 0 minutes past every hour

If you want to combine that with more tasks in the same script the sleep is not an option, in that case you could something like this:

import time
interval = 3600
lasttime = time.time()

# This is supposed to run repeatedly inside your main loop
now = time.time()
if ((lasttime - now)>interval):
  lasttime = now
  doTask()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top