可以使用Python的time模块和open函数来实现每隔一秒读取一次文件。具体步骤如下:
open函数打开要读取的文件,可以指定文件的路径和打开模式(如'r'表示只读模式)。time.sleep(1)函数来暂停一秒钟,以实现每隔一秒读取一次文件。read方法来读取文件内容,并打印出来。下面是一个示例代码:
import time filename = 'path/to/file.txt' # 替换为实际的文件路径 while True: with open(filename, 'r') as file: content = file.read() print(content) time.sleep(1)