Python植入了一个while循环,用于检查文件是否包含新数据
我试图编写一个while true循环,定期(每10秒)检查文本文件的最后一行-Python植入了一个while循环,用于检查文件是否包含新数据,python,python-3.x,file-handling,Python,Python 3.x,File Handling,我试图编写一个while true循环,定期(每10秒)检查文本文件的最后一行-TransactionFile.txt,其中包含附加到文件的事务数据 基本上我有两个程序,一个将用户事务附加到文件中,另一个读取事务,这两个程序都同时运行 我遇到的问题是确定TransactionFile.txt中的当前事务是否已更改 这是我的职责 def transactionCheck(): threading.Timer(10.0, transactionCheck).start() fileS
TransactionFile.txt
,其中包含附加到文件的事务数据
基本上我有两个程序,一个将用户事务附加到文件中,另一个读取事务,这两个程序都同时运行
我遇到的问题是确定TransactionFile.txt
中的当前事务是否已更改
这是我的职责
def transactionCheck():
threading.Timer(10.0, transactionCheck).start()
fileSecLast = None
while True:
try:
file = open('TransactionFile.txt', 'r')
except FileNotFoundError:
print('TransactionFile.txt could not found.')
break
else:
file = open('TransactionFile.txt', 'r')
global fileLast
fileLast = file.readlines()[-1]
file.close
if fileLast == fileLast:
print('New transaction data found.')
print(fileLast)
callfunctionNonce = calcNonce()
elif fileLast == fileSecLast:
print('No new transaction data found.')
break
break
break
它给出了这个的输出:
TransactionFile.txt could not found.
TransactionFile.txt could not found.
TransactionFile.txt could not found.
New transaction data found.
MOWZ575728::max::sam::67890::2018-05-19 13:11:50.250116
New transaction data found.
MOWZ575728::max::sam::67890::2018-05-19 13:11:50.250116
New transaction data found.
MOWZ575728::max::sam::67890::2018-05-19 13:11:50.250116
New transaction data found.
DOYU363556::geoff::tim::14515::2018-05-19 09:12:06.250116
我想要实现的是,如果没有新的事务写入TransactionFile.txt
output中,则找不到新的事务数据。
而不是再次打印相同的事务。我该怎么做呢
我的预期输出应该是这样的:
TransactionFile.txt could not found.
TransactionFile.txt could not found.
New transaction data found.
MOWZ575728::max::sam::67890::2018-05-19 09:11:50.250116
No new transaction data found.
No new transaction data found.
New transaction data found.
DOYU363556::geoff::tim::14515::2018-05-19 09:12:06.250116
感谢您的反馈和帮助
如果我的问题不清楚,我会尽力以不同的方式解释。你这样做是为了让事情变得复杂。如果您能够设法确保文件从一开始就退出,则应执行以下操作:
def transactionCheck():
file = open("TransactionFile.txt", 'r')
while True:
line = file.readline().strip()
if line:
print("New transaction:")
print(line)
print()
else:
print("No new transaction")
time.sleep(10)
您从未给
filesecast
赋值,并且if条件为not true。试试这个:
def transactionCheck():
threading.Timer(10.0, transactionCheck).start()
fileSecLast = None
while True:
try:
file = open('TransactionFile.txt', 'r')
except FileNotFoundError:
print('TransactionFile.txt could not found.')
break
else:
file = open('TransactionFile.txt', 'r')
global fileLast
fileLast = file.readlines()[-1]
file.close
if fileLast == fileSecLast:
print('No new transaction data found.')
break
else:
print('New transaction data found.')
print(fileLast)
callfunctionNonce = calcNonce()
fileSecLast = fileLast
break
break
可能会在每个事务的开始或结束处添加一个“New transaction”标志,然后在从事务数据中删除该标志后仅返回带有该标志的事务。如果fileLast==fileLast,则将变量与其自身进行比较,这始终是真的,因此即使没有新事务,您得到的是错误的输出。您应该为当前迭代使用一个新变量,例如-
recentFileLast
,并与以前的fileLast
进行比较,以确定是否存在任何更改。其他程序是否仅追加或完全覆盖该文件?另外,最好有一个最小的其他程序,这样我们就可以进行测试了。为什么会有而True
循环呢?您正在立即脱离它。@扩展其他程序会将新事务附加到文件中。