Python 检查文件';在一段时间内保持可用性,然后中断
我一直在尝试打破一个循环,这意味着在某个位置查找文件。我的意图是让我的脚本在一定时间内查找该文件,然后中断是否找到该文件,但我不知道 如何让脚本等待一段时间,然后在时间结束时中断? 这是我此刻的剧本:Python 检查文件';在一段时间内保持可用性,然后中断,python,python-3.x,time,Python,Python 3.x,Time,我一直在尝试打破一个循环,这意味着在某个位置查找文件。我的意图是让我的脚本在一定时间内查找该文件,然后中断是否找到该文件,但我不知道 如何让脚本等待一段时间,然后在时间结束时中断? 这是我此刻的剧本: 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 th
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存在\u超时(路径,超时):
“”“Return一旦存在,或在秒之后,
以较早者为准
"""
定时器=超时
而(不是os.path.exists(path))和计时器>0:
时间。睡眠(1)
计时器-=1
def存在\u超时(路径,超时):
“”“Return一旦存在,或在秒之后,
以较早者为准
"""
定时器=超时
而(不是os.path.exists(path))和计时器>0:
时间。睡眠(1)
计时器-=1
通过使用time.time()
函数执行实际时间
减去开始时间
计算经过的时间,并分配一个变量(文件\u在此代码中存在
),该变量将被修改,并检查文件是否存在,并将其用于循环
详情如下:
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")
通过使用time.time()
函数执行actual time
减去start time
计算经过的时间,并分配一个变量(file\u在此代码中存在
),该变量将被修改,并检查文件是否存在,并将其用于循环
详情如下:
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")
导入操作系统
导入时间
文件\u path=r“C:\Users\WCS\Desktop\item.txt”
cTime=0
时限=5
而cTime导入操作系统
导入时间
文件\u path=r“C:\Users\WCS\Desktop\item.txt”
cTime=0
时限=5
正如roganjosh所评论的,cTime,如果使用时间戳会更简单。我在下面添加了相关代码:
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)
导入操作系统
导入时间
从datetime导入datetime,timedelta
文件\u path=r“C:\Users\WCS\Desktop\item.txt”
time_limit=datetime.now()+timedelta(秒=5)
present=datetime.now()
而(不是os.path.exists(path))和present<时限:
present=datetime.now()
如果存在>=时间限制:
打印(“时间到了”)
打破
时间。睡眠(1)
正如roganjosh所评论的,如果使用时间戳会更简单。我在下面添加了相关代码:
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)
导入操作系统
导入时间
从datetime导入datetime,timedelta
文件\u path=r“C:\Users\WCS\Desktop\item.txt”
time_limit=datetime.now()+timedelta(秒=5)
present=datetime.now()
而(不是os.path.exists(path))和present<时限:
present=datetime.now()
如果存在>=时间限制:
打印(“时间到了”)
打破
时间。睡眠(1)
以下是如何使用该类完成此操作。这些可以配置为延迟指定的时间量,并根据您的选择延迟调用
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")
下面是如何在课堂上做到这一点。这些可以配置为延迟指定的时间量,并根据您的选择延迟调用
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")
要检查某个位置的文件是否可用,可以尝试以下操作。脚本将在找到文件后立即中断,否则将在中断前最多等待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")
要检查某个位置的文件是否可用,可以尝试以下操作。脚本将在找到文件后立即中断,否则将在中断前最多等待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")
如果你使用实际的时间戳,那就简单了。在循环之前获取时间戳并比较循环中的当前时间戳。下面的所有解决方案都会强制脚本等待该时间限制
达到或超过,即使文件存在。难道脚本不可能在找到文件后立即中断吗?否则它将等待到时间限制
?谢谢。如果你用实际的时间戳,那就简单了。在循环之前获取时间戳并比较循环中的当前时间戳。下面的所有解决方案都会强制脚本等待该时间限制
达到或超过,即使文件存在。难道脚本不可能在找到文件后立即中断吗?否则它将等待到时间限制
?谢谢