i have the code below and my problem is the third for-loop doesn't start. This part should compare ids in history file with ids from json_result list and then download or not. What do i do wrong and how to fix it?
today = datetime.datetime.today().strftime('%d.%m')
for day in var.red_letter_days.keys():
if today == day:
api = AppPixivAPI()
api.auth(refresh_token=var.refresh_token)
json_result = api.search_illust(f'{var.red_letter_days[today]}', search_target='partial_match_for_tags')
with open(var.history_file, 'a ', encoding='utf-8') as f:
for idx, illust in enumerate(json_result.illusts):
for i in f.readlines():
if i == illust.id:
print(illust.id)
break
else:
f.write(str(i) '\n')
download(api, idx, illust)
CodePudding user response:
You've opened the file in append mode, with the 'a'
option (the ' '
part opens for reading and writing, but it's still opened in append mode). This means the file is opened at the end of the file, so data can be appended. Reading from the end of a file will result in nothing, so f.readlines()
will be empty, and for i in f.readlines()
will be an empty loop.