我有一个简单的web抓取python脚本,它将结果写入.csv文件。我只想在.csv文件不包含今天的日期时添加一个条目。目前,这位作家根本没有写作。
with open('results.csv', mode='a') as csv_file:
write = True
for line in csv_file:
if line.contains(date.today()):
write = False
if write == True:
writer = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow([date.today(),mainstream_count,specialist_count])发布于 2021-05-15 20:28:46
打开文件前检查文件上的修改时间戳。
import csv
import datetime
import os
file_name = "results.csv"
modified = datetime.date.fromtimestamp(os.path.getmtime(file_name))
if (modified < datetime.date.today()):
with open(file_name, mode='a') as csv_file:
writer = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow([date.today(),mainstream_count,specialist_count])发布于 2021-05-15 20:29:43
您可以使用pathlib获取文件修改时间。
import time
import pathlib
import csv
file_path = pathlib.Path('results.csv')
modification_time = file_path.stat().st_mtime
current_time = time.time()
# Only write if it wasn't modified in the last 8 hours.
if current_time - modification_time > 60 * 60 * 8:
with open(file_path.name, mode='a') as csv_file:
writer = csv.writer(
csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL
)
writer.writerow([date.today(),mainstream_count,specialist_count])有了这个逻辑,您还需要确保该文件在stat之前存在。我会把这个变成它自己的功能。
def recently_updated(file_path, hours):
if not file_path.exists():
return False
mtime = file_path.stat().st_mtime
return time.time() - mtime < 60 * 60 * hours
...
if not recently_updated(file_path, hours=8):
with ...https://stackoverflow.com/questions/67550834
复制相似问题