AngiSen
AngiSen

Reputation: 997

Python - How to run script continuously to look for files in Windows directory

I got a requirement to parse the message files in .txt format real time as and when they arrive in incoming windows directory. The directory is in my local Windows Virtual Machine something like D:/MessageFiles/

I wrote a Python script to parse the message files because it's a fixed width file and it parses all the files in the directory and generates the output. Once the files are successfully parsed, it will be moved to archive directory. Now, i would like to make this script run continuously so that it looks for the incoming message files in the directory D:/MessageFiles/ and perform the processing as and when it sees the new files in the path.

Can someone please let me know how to do this?

Upvotes: 2

Views: 3288

Answers (2)

BernardL
BernardL

Reputation: 5464

You might want to check out the time function to add a delay and keep parsing files: Roughly something like:

from time import sleep
import os

while 1:
    print(os.listdir('/.')) #do something here. in this sample, it prints the current directory 
    sleep(60) #delay for 60 seconds before it goes back to do something

Upvotes: 1

Alexis Drakopoulos
Alexis Drakopoulos

Reputation: 1145

There are a few ways to do this, it depends on how fast you need it to archive the files.

If the frequency is low, for example every hour, you can try to use windows task scheduler to run the python script.

If we are talking high frequency, or you really want a python script running 24/7, you could put it in a while loop and at the end of the loop do time.sleep()

If you go with this, I would recommend not blindly parsing the entire directory on every run, but instead finding a way to check whether new files have been added to the directory (such as the amount of files perhaps, or the total size). And then if there is a fluctuation you can archive.

Upvotes: 3

Related Questions