我正在用.log文件中的数据创建一个SQLite数据库。计算机每次执行操作时都会更新日志文件。我在Windows中所做的是监视这个文件的变化,一旦它被修改——外部由机器通过串行通信,但我通过在记事本中打开文件并添加新行来模拟这种行为——读取新行并将数据放入数据库。你知道吗
问题是当我尝试对我的Linux机器做同样的操作时,因为显然python程序在文件中看不到任何修改,即使在更新和保存之后。尽管如此,如果我关闭并重新打开程序中的文件以重新读取它,修改仍然存在(但我仍然无法检测到新的修改)。你知道吗
实际上,我已经在运行在Raspbian上的Raspberry Pi 3 B+上测试了相同的代码,并且它按预期工作:检测到修改,并将数据添加到数据库中。你知道吗
.log文件的格式如下所示。你知道吗
01 (PB) 008 Pa 6.01 bar 12/11/2055 07:25:17 01 (PB) 008 Pa 6.01 bar 12/11/2055 07:26:39 01 (PB) 009 Pa 6.00 bar 12/11/2055 07:29:45
下面是我使用的代码:
import re
import sqlite3
import os, time
if __name__=='__main__':
fd = open('putty.log', 'r')
# Ici on crée la base de données qui gardera les résultats des tests
connection = sqlite3.connect('banc_tests.db')
cursor = connection.cursor()
create_db = """
CREATE TABLE tests (
reference VARCHAR(15),
num_serie VARCHAR(15),
operation TINYINT,
resultat CHAR(2),
fuite SMALLINT,
unite VARCHAR(5),
pression FLOAT,
pUnite VARCHAR(5),
date DATE,
time TIME
);
"""
cursor.execute(create_db)
# Here we look for the testing bank frame's format with regular expressions
# For now we are ignoring all information about low pressure, PE test, etc.
while 1:
where = fd.tell() # Garde la position actuelle du curseur dans le fichier
line = fd.readline() # On lit la ligne suivante
if not line:
time.sleep(1)
fd.seek(where) #Si il n'y a rien à lire on retourne à la position précédente.
else:
# The ([\+\-]?\d*)\s*(\w*) segment in intended to ignore whitespaces in the faulty testing lines
# as well as to grab the leak pressure for ordinary tests. To change it back to the original
# version, it suffices to replace every * by a +.
print(line)
found = re.findall(r'^\s+(\d+)\s+\((\w+)\)\s+([\+\-]?\d*)\s*(\w*)\s+(\d+\.?\d+)\s+(\w+).*\s(\d+)/(\d+)/(\d+)\s+(\d+:\d+:\d+)', line)
# La commande findall retourne une tuple dans une liste, donc ici j'enlève la tuple et laisse
# juste la liste avec les éléments dedans.
if found:
temp = list(found[0])
insert_values = """
INSERT INTO tests (reference, num_serie, operation, resultat, fuite, unite, pression, pUnite, date, time)
VALUES ("{ref}", "{num_ser}", "{op}", "{res}", "{fuite}", "{unt}", "{pression}", "{pUnt}", "{date}", "{time}"); """
insert_command = insert_values.format(ref='', num_ser='', op=temp[0], res=temp[1], fuite=temp[2], unt=temp[3], pression=temp[4], pUnt=temp[5], date='{}-{}-{}'.format(temp[8], temp[7], temp[6]), time=temp[9])
cursor.execute(insert_command)
connection.commit()
我认为这是open()函数的阻塞行为的问题,所以我尝试将os.O\u NONBLOCK标志与fcntl一起使用;但是没有起作用。你知道吗
有人知道是什么导致了Linux(debian10bluster)和Windows之间的这种不同行为吗?我能做些什么来解决Linux方面的问题呢?你知道吗
我做了一些研究,发现了问题所在。这是因为在编辑器中保存后,文件的
inode
已更改。你知道吗您可以使用
vim
和设置set backupcopy=yes
来防止这种行为(http://vimdoc.sourceforge.net/htmldoc/options.html#'backupcopy')要观察这种行为,可以使用
ls -li
,它将显示文件的inode
。你知道吗第一列显示
inode
。正如您在vim编辑之后看到的,它已经改变了,这就是为什么您的脚本看不到这些改变(因为编辑器覆盖了这个文件)。你知道吗相关问题 更多 >
编程相关推荐