Reputation: 137
I've been trying to break a loop which is meant to look for a file in a certain location. My intention is to make my script look for that file for a certain time and then break whether the file is found or not but I can't get any idea.
How can I make the script wait for a certain time and then break when the time is up?
This is my script at this moment:
import os
import time
file_path = r"C:\Users\WCS\Desktop\item.txt"
time_limit = 5
while not os.path.exists(file_path):
time.sleep(1)
#is there any logic I can apply here to make the following line valid
# if waiting_time>=time_limit:break
print("Time's up")
Upvotes: 0
Views: 779
Reputation: 2542
Calculate the elapsed time by doing actual time
minus start time
by using time.time()
function and assign a variable (file_exists
in this code) which will be modified and check whether the file exist or not and use it for the loop.
As below:
import os
import time
file_path = r"C:\Users\WCS\Desktop\item.txt"
time_limit = 5
start = time.time()
file_exists = os.path.exists(file_path)
while not file_exists:
time.sleep(1)
file_exists = os.path.exists(file_path)
elapsed = time.time() - start
if elapsed >= time_limit:
break
else:
print("File exist.")
print(elapsed)
print("Time's up")
Upvotes: 2
Reputation: 22440
To check for the availability of a file in a certain location you can try the following. The script will break as soon as the file is found otherwise it will wait upto 5 seconds for the file to be available before breaking.
import os
import time
file_path = r"C:\Users\WCS\Desktop\item.txt"
time_to_wait = 5
time_counter = 0
while not os.path.exists(file_path):
time.sleep(1)
time_counter += 1
if time_counter > time_to_wait:break
print("done")
Upvotes: 0
Reputation: 123511
Here's how to do it with the threading.Timer()
class. These can be configured to delay a specified amount of time and the call as function of your choosing.
import os
from threading import Timer
import time
file_path = r"C:\Users\WCS\Desktop\item.txt"
# Timer callback function.
def timeout():
global time_ran_out
time_ran_out = True
time_limit = 5
time_ran_out = False # Define variable the callback function modifies.
timer = Timer(time_limit, timeout) # Create a timer thread object.
timer.start() # Start the background timer.
while not os.path.exists(file_path):
time.sleep(1)
if time_ran_out:
print('Times up!')
break
print("Done")
Upvotes: 0
Reputation: 82
As roganjosh commented, it would be simpler if you used time stamps. I have added relevant code below:
import os
import time
from datetime import datetime, timedelta
file_path = r"C:\Users\WCS\Desktop\item.txt"
time_limit = datetime.now() + timedelta(seconds=5)
present = datetime.now()
while (not os.path.exists(path)) and present < time_limit:
present = datetime.now()
if present >= time_limit:
print("Time's up")
break
time.sleep(1)
Upvotes: 0
Reputation: 919
import os
import time
file_path = r"C:\Users\WCS\Desktop\item.txt"
cTime=0
time_limit = 5
while cTime<time_limit:
if os.path.exists(file_path)==False:
cTime=cTime+1
time.sleep(1)
else:
pass
if cTime==5:
responce="Time's Up"
else:
responce='Found'
print(responce)
Upvotes: 0
Reputation: 616
def exists_timeout(path, timeout):
"""Return once <path> exists, or after <timeout> seconds,
whichever is sooner
"""
timer = timeout
while (not os.path.exists(path)) and timer > 0:
time.sleep(1)
timer -= 1
Upvotes: 1