A simple implementation could be:
import os.path
import time
while not os.path.exists(file_path):
time.sleep(1)
if os.path.isfile(file_path):
# read file
else:
raise ValueError("%s isn't a file!" % file_path)
You wait a certain amount of time after each check, and then read the file when the path exists. The script can be stopped with the KeyboardInterruption
exception if the file is never created. You should also check if the path is a file after, to avoid some unwanted exceptions.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…