猿问

Python - 如何打开 n 个不同的 json 文件并进行追加。一次

我想要做的是,我希望能够打印 n 次待定文件夹中有多少 json 文件,同时它会为每个 json 打印所有这些数据,但它应该只添加到 old_list 一次。


我编码的代码如下:


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)

我现在遇到的问题是,根据我拥有的 json 文件数量以及我想要制作的内容,它正在将 n 次添加到列表中,它应该打印出所有的 json 但只添加到列表一次而不是 n次。


在这种情况下,我的问题是:我怎样才能只添加一次列表但仍然能够打印出所有这些 jsons?


开心每一天1111
浏览 215回答 1
1回答
随时随地看视频慕课网APP

相关分类

Python
我要回答