[英]Check for a file's availability for a certain time and then break
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")
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
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. 通过使用
time.time()
函数执行actual time
减去 start time
来计算经过的时间,并分配一个将被修改的变量(此代码中为file_exists
),并检查文件是否存在并将其用于循环。
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")
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)
As roganjosh commented, it would be simpler if you used time stamps. 正如roganjosh所评论的那样,如果您使用时间戳记,它将更加简单。 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)
Here's how to do it with the threading.Timer()
class. 这是使用
threading.Timer()
类进行操作的方法。 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")
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.
一旦找到文件,脚本将立即中断,否则将等待5秒钟,直到文件可用为止。
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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.