以下代码将显示在文件夹中创建的最新文件的名称及其内容,并且还将每30秒继续打印一次。
import glob
import os
import time
while True:
newest=max(glob.iglob('/Users/BetaBrawler/Downloads/HernanVillela/*'), key=os.path.getctime)
print newest
file = open(newest,'r')
texto = [x.strip() for x in file.readlines()]
print texto
time.sleep(30)
我唯一想做的是,如果以下输出与前一个输出具有相同的值(相同的文件及其内容),控制台将停止打印,并且只有在文件夹中创建新文件时才会打印。 / p>
答案 0 :(得分:0)
import glob
import os
import time
oldfile = None
while True:
newest = max(glob.glob('/Users/BetaBrawler/Downloads/HernanVillela/*'), key=os.path.getctime)
if newest == oldfile:
time.sleep(30)
continue
oldfile = newest
with open(newest) as infile:
print [line.strip() for line in infile]
time.sleep(30)
答案 1 :(得分:0)
import glob
import os
import time
current = None
while True:
newest = max(glob.iglob('/Users/BetaBrawler/Downloads/HernanVillela/*'), key=os.path.getctime)
if newest != current:
current = newest
print newest
with open(newest, 'r') as file_:
texto = [x.strip() for x in file_.readlines()]
print texto
time.sleep(30)