Reputation: 315
What I am trying to do is that I want be able to print out n times pending on how many json files I have in the folder while it prints out all these data for each json BUT it should only add to old_list once.
My code I have coded is following:
old_list = ['Hello', 'How', 'Are', 'You']
new_list = ['Im', 'Fine', 'Today', 'You']
while True:
if new_list not in old_list:
directory = os.fsencode('./slack')
for counters, file in enumerate(os.listdir(directory)):
filename = os.fsdecode(file)
if filename.endswith(".json"):
with open('./slack/' + filename) as slackAttachment:
data = json.loads(slackAttachment.read())
data_list = []
data["attachments"][0]["footer"] = str(
data["attachments"][0]["footer"] + ' | ' + datetime.now().strftime(
'%Y-%m-%d [%H:%M:%S.%f')[:-3] + "]")
# -------------------------------------------------------------------------
print(data)
old_list.append(new_list)
The problem right now I am having is that it is adding to the list n times depending on how many json files I have and what I want to make is that it should print out all the json but only add to the list once instead of n times.
My question in that case is: How can I be able to add the list only once but still be able to print out all these jsons?
Upvotes: 0
Views: 88
Reputation: 1350
The problem is your while True
which causes your code print out non stop. Moreover, if new_list not in old_list:
is not a right way to compare two lists:
old_list = ['Hello', 'How', 'Are', 'You']
new_list = ['Im', 'Fine', 'Today', 'You']
data_list = []
directory = os.fsencode('./slack')
for newLst in new_list:
if newLst in old_list:
for counters, file in enumerate(os.listdir(directory)):
filename = os.fsdecode(file)
if filename.endswith(".json"):
with open('./slack/' + filename) as slackAttachment:
data = json.loads(slackAttachment.read())
data["attachments"][0]["footer"] = str(
data["attachments"][0]["footer"] + ' | ' + datetime.now().strftime(
'%Y-%m-%d [%H:%M:%S.%f')[:-3] + "]")
print(data)
data_list.append(newLst) # make sure to what you're ganna append
Note that I'm assuming the rest of your code is working well.
Upvotes: 1