尝试编写一个每天创建一个新 json 文件的 python 代码



我正在处理人脸识别模型的json日志,我的任务是编写一个每天动态创建新文件的代码。我有一个代码,但不确定为什么它只写第一个日志。我希望它不断追加,只要我的相机继续识别人脸。

这是我的代码:

from datetime import datetime,timedelta
import os
from pprint import pprint
import json
yesterday = datetime.now() - timedelta(days=1)
yesterday1 = datetime.strftime(yesterday, '%Y%m%d')
yesterday_str = str(yesterday1)
now1 = datetime.strftime(datetime.now(), '%Y%m%d')
now1_str = str(now1)
def write_logs(time,date,name,accuracy,direction):
entry = {'time':time,'name':name,'accuracy':accuracy,'direction':direction}
yesterday_log_file = './log'+yesterday_str+'.json'
log_file = './log'+now1_str+'.json'
if os.path.exists(yesterday_log_file):
with open(yesterday_log_file) as f:
Date = json.load(f)
Date1 = (Date[-1])
Comparision_Date = Date1['time']
a = datetime.strptime(Comparision_Date[:10],'%d/%m/%Y')
print(a)
now = datetime.strptime(datetime.now(),'%d/%m/%Y')
if a == now:
with open(yesterday_log_file, 'r') as r:
data = json.load(r)
data.append(entry)
with open(log_file, mode='w') as f:
json.dump(data, f, indent=3)    
if a < now:
# Create file with JSON enclosures
with open(log_file, mode='w') as f:
json.dump([], f)
# The file already exists, load and update it
with open(log_file, 'r') as r:
data = json.load(r)
data.append(entry)
# Write out updated data
with open(log_file, mode='w') as f:
json.dump(data, f, indent=3)
else:
# Create file with JSON enclosures
with open(log_file, mode='w') as f:
json.dump([], f)
# The file already exists, load and update it
with open(log_file, 'r') as r:
data = json.load(r)
data.append(entry)
# Write out updated data
with open(log_file, mode='w') as f:
json.dump(data, f, indent=3)

return [entry]

但是,让我告诉你,它适用于单个 if 语句,正如 @T.Ray 在这里提到的:在尝试将 Python 字典附加到 JSON 时,它只写入一次

mode='w'更改为mode='a'

  • w覆盖任何现有文件
  • a只是附加在现有文件上(如果不存在,则创建一个(

这里有很多好信息: http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python

如果要在每天午夜创建新的日志文件,则不需要比较日期。 如果是午夜,则新日期将始终大于旧日期(即昨天(。所以比较似乎没有意义。 如果是新的一天,write_logs将自动创建新的日志文件。现在,如果您需要创建一个新的日志文件(在午夜(,无论您是否有要写入的条目,那么您可以编写一个包装函数来处理它:

def update_logs(args=None):
# If no entry is passed, create new log file
if not args:
log_file = './log'+now_str+'.json'
if not os.path.exists(log_file):
# Create file with JSON enclosures
with open(log_file, 'w') as f:
json.dump([], f)
else:
# A new entry is passed, update existing log file
write_logs(*args)

def write_logs(time, date, name, accuracy, direction):
entry = {'time': time,
'name': name,
'accuracy': accuracy,
'direction': direction}
log_file = './log'+now_str+'.json'
if not os.path.exists(log_file):
# Create file with JSON enclosures
with open(log_file, 'w') as f:
json.dump([], f)
# The file already exists, load and update it
with open(log_file, 'r') as r:
data = json.load(r)
data.append(entry)
# Write out updated data
with open(log_file, 'w') as f:
json.dump(data, f, indent=3)
return [entry]
# Example records
entries = [("18/06/2018 - 20:39:07", 'whatever', "Rajkiran", "97.22941", 'default'),
("18/06/2018 - 20:39:07", 'whatever', "Rajkiran", "97.22941", 'default'),
("13/06/2018 - 20:39:07", 'whatever', "Rajkiran", "97.22941", 'default'),
("13/06/2018 - 20:39:07", 'whatever', "Rajkiran", "97.22941", 'default'),
("13/06/2018 - 20:39:07", 'whatever', "Rajkiran", "97.22941", 'default'),
("13/06/2018 - 20:39:07", 'whatever', "Rajkiran", "97.22941", 'default')]

# Case 1: Log file already exists, update it
now = datetime.strftime(datetime.now(), '%Y%m%d')
now_str = str(now)
for entry in entries:
update_logs(entry)
# Case 2: Midnight, no entries, create a new empty log file
now = datetime.strftime(datetime.now() + timedelta(days=1), '%Y%m%d')
now_str = str(now)
update_logs()
# Case 3: Midnight, with entries to write
now = datetime.strftime(datetime.now() + timedelta(days=1), '%Y%m%d')
now_str = str(now)
for entry in entries:
update_logs(entry)

打电话给update_logs将为您处理一切。

相关内容

最新更新