Hi,

Very new to Python and still stumbling around but need some help.

As part of a larger script, I'd like to run a routine that checks for the existence of a file once a second for about 3 minutes and if it suddenly appears, carry on with the rest of the script but if its not there, keep on looking until the 3 minutes are up (and then stop processing any more of the script).

I know I can use

os.path.isfile

to check and it must be some kind of while/for loop but just can't get my head round what i need.

Any help would be most appreciated.

Cheers,

Mattokun

Recommended Answers

All 2 Replies

You could write something like

from time import sleep
import os

def wait_file(filename):
    cnt = 3 * 60
    while cnt > 0:
        if os.path.isfile(filename):
            return
        cnt -= 1
        sleep(1)
    raise SystemExit

Fantastic - all works fine! Many thanks for your help Gribouillis!!

Cheers,

Matt

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.