监视文件是否停止在python中写入 [英] Monitoring if a file stopped writing in python
问题描述
try:
if time.time()>(os.stat(filename).st_mtime + 2):
raise ValueError(Yikes!Spike )
除了ValueError:
以open('errors.log','a')作为日志:
log.write('Spike发生在'+ time.strftime(
('%d /%m /%Y')+'\ n')
log。 close()
restart_program()
该块每秒运行一次。但是,这种情况并没有发生,当应用程序关闭重新启动时,它每秒关闭,不会再次启动。我得到每秒记录的异常消息。我试图增加时间差,但没有帮助。
接下来我试了一下
ftimestamp = os.stat(filename).st_mtime
try:
if os.stat(filename).st_mtime> = ftimestamp:
ftimestamp = time.time()
printftimestamp updated and all is well
else:
ftimestamp = os.stat(filename).st_mtime
提高ValueError(Yikes!Spike!)
打印文件时间在后面
除了ValueError:
打开('errors.log','a')作为日志:
log.write('Spike发生在+时间。 strftime(
%H:%M:%S)+'on'+ datetime.date.today()。strftime('%d /%m /%Y')+'\\\
')
log.close()
restart_program()
ftimestamp为当前时间time.time(),因为下一次比较只发生在一秒之后,而且我希望文件时间比前一次更高上。 (块通过wx.CallLater函数每秒运行)。
我的程序仍然失败...我无法理解我要去哪里错误...有人请帮忙!或者有没有简单的方法来检查文件是否停止了写入?
导入os
从时间导入睡眠
#其他导入
而真:
file1 = os.stat('file.txt')#初始文件大小
file1_size = file1.st_size
#你的脚本在这里收集和写入数据(增加文件大小)
sleep(1)
file2 = os.stat('file.txt')#更新文件大小
file2_size = file2.st_size
comp = file2_size - file1_size#比较大小
如果comp == 0:
restart_program()
else:
sleep(5)
您可能需要调整 sleep()
函数,因为我无法测试你的实际代码。最后,这是一个无限循环,只要您希望脚本继续写就可以继续运行。
另一个解决方案是更新您的
import os
import sys
from time导入睡眠
#其他导入
而真:
file1 = os.stat('file.txt')#初始文件大小
file1_size = file1.st_size
#这里收集和写入数据的脚本(增加文件大小)
sleep(1)
file2 = os.stat('file.txt')#更新文件大小
file2_size = file2.st_size
comp = file2_size - file1_size#比较大小
如果comp == 0:
sys.exit
else:
sleep(5)
$ c
$ b然后使用一个辅助程序来运行你的脚本:
pre > 导入os
从时间导入sleep,strftime
而真:
print(strftime(%H:%M:%S),开始))
系统( main.py')#这是另一个无限循环,将使脚本运行
print(strftime(%H:%M:%S),Crashed))
sleep(5)
I have a program that keeps writing to a file every second. File writing is happening in a thread parallel to the UI. Due to some hardware issue it stops writing sometimes of a day. I wanted to check if the file stopped writing, restart the program if it is not getting updated. I wanted to check the file's timestamp and see if it is not getting updated(and did not want to get to watchdog etc. coz I just needed if a file stopped writing.)
try:
if time.time()>(os.stat(filename).st_mtime+2):
raise ValueError("Yikes! Spike")
except ValueError:
with open('errors.log','a') as log:
log.write('Spike occured at '+ time.strftime(
"%H:%M:%S")+' on '+datetime.date.today().strftime('%d/%m/%Y')+'\n')
log.close()
restart_program()
This block runs every second. But this backfired and when the app closes for restarting it keeps closing every second and doesn't start again. I get the exception message logged every second. I tried increasing the time difference but that didn't help.
Next I tried
ftimestamp = os.stat(filename).st_mtime
try:
if os.stat(filename).st_mtime>=ftimestamp:
ftimestamp = time.time()
print "ftimestamp updated and all is well"
else:
ftimestamp = os.stat(filename).st_mtime
raise ValueError("Yikes! Spike!")
print "file time is behind"
except ValueError:
with open('errors.log','a') as log:
log.write('Spike occured at '+ time.strftime(
"%H:%M:%S")+' on '+datetime.date.today().strftime('%d/%m/%Y')+'\n')
log.close()
restart_program()
I tried updating the variable "ftimestamp" to current time "time.time()" because the next comparison happens only after one second and I want the file time to be higher than the previous time comparison. (The block runs every second through wx.CallLater function).
My program fails still... and I am not able to understand where I am going wrong... Someone please help! Or is there a way of simply checking if the file stopped writing?
We can try checking for change in file size as a possible solution by doing the following:
import os
from time import sleep
# other imports
while True:
file1 = os.stat('file.txt') # initial file size
file1_size = file1.st_size
# your script here that collects and writes data (increase file size)
sleep(1)
file2 = os.stat('file.txt') # updated file size
file2_size = file2.st_size
comp = file2_size - file1_size # compares sizes
if comp == 0:
restart_program()
else:
sleep(5)
You may need to adjust the sleep()
function accordingly these are just estimates that I'm using since I can't test your actual code. In the end this is infinite loop that will keep running as long as you want the script to keep writing.
Another solution is to update your file to:
import os
import sys
from time import sleep
# other imports
while True:
file1 = os.stat('file.txt') # initial file size
file1_size = file1.st_size
# your script here that collects and writes data (increase file size)
sleep(1)
file2 = os.stat('file.txt') # updated file size
file2_size = file2.st_size
comp = file2_size - file1_size # compares sizes
if comp == 0:
sys.exit
else:
sleep(5)
Then use a secondary program to run your script as such:
import os
from time import sleep, strftime
while True:
print(strftime("%H:%M:%S), "Starting"))
system('main.py') # this is another infinite loop that will keep your script running
print(strftime("%H:%M:%S), "Crashed"))
sleep(5)
这篇关于监视文件是否停止在python中写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!