Make Python wait until a file exists before continuing
Question: In my code, I write a file to my hard disk. After that, I need to import the
generated file and then continue processing it.

    
    
    for i in xrange(10):
        filename=generateFile()
        # takes some time, I wish to freeze the program here
        # and continue once the file is ready in the system
        file=importFile(filename)
        processFile(file)
    

If I run the code snippet in one go, most likely `file=importFile(filename)`
will complain that that file does not exist, since the generation takes some
time.

I used to manually run `filename=generateFile()` and wait before running
`file=importFile(filename)`.  
Now that I'm using a `for` loop, I'm searching for an automatic way.


Answer: You could use `time.sleep` and I would expect that if you are loading a module
this way you would need to `reload` rather than `import` after the first
`import`.

However, unless the file is very large why not just generate the string and
then `eval` or `exec` it?

**Note** that since your file generation function is not being invoked in a
thread it should be blocking and will only return when it thinks it has
finished writing - possibly you can improve things by ensuring that the file
writer ends with `outfile.flush()` then `outfile.close()` but on some OSs
there may still be a time when the file is not actually available.

