[python] Save json

Asked 2 years ago, Updated 2 years ago, 41 views

def lnktojson(lnkdata_path):

    file_list = glob.glob(os.path.join(lnkdata_path, '*.lnk'))

    json_data = []

    for f_idx in range(len(file_list)):
        try:
            #save json data
            file = Lnk.file_open(file_list[f_idx])
            file.show_all_info()
            json_data.append(file.get_all_info())

            print("Save Complete\n\n")

        except:
                print("\n")

    with open('lnk.json', 'w') as json_file:
        json.dump(json_data, json_file, indent=4)



#main
if __name__=="__main__":
    print("Enter the path where the file to analyze is located.\n")
    lnkdata_path=input ("path :")

    lnktojson(lnkdata_path)

If you save it like this,

[
    [
        {
            "File Attributes0": "FILE_ATTRIBUTE_ARCHIVE",
            "Target File Creation TimeZone": "UTC",
            "Target File Creation Time": "2020-12-03 00:25:00",
            "Target File Access TimeZone": "UTC",
            "Target File Access Time": "2020-12-06 09:36:33",
            "Target File Write TimeZone": "UTC",
            .
            .
            .
            "Target File Size": "123542720bytes",
            "IconIndex": "0",
            "Show Command": "SW_SHOWNORMAL",
            "Drivetype": "DRIVE_FIXED",
            "Driveserialnumber": "ef31ee58",
            "Volumelable": "",
            "Localbasepath Unicode": "None",
            "Localbasepath": "C:\\Users\\User\\AppData\\Local\\Programs\\Evernote\\Evernote.exe",
            .
            .
            .
        }
    ],
    [
        {
            "File Attributes0": "FILE_ATTRIBUTE_ARCHIVE",
            "Target File Creation TimeZone": "UTC",
            "Target File Creation Time": "2021-03-25 09:30:36",
            "Target File Access TimeZone": "UTC",
            "Target File Access Time": "2021-03-25 09:30:37",
            "Target File Write TimeZone": "UTC",
            .
            .
            .
            "Target File Size": "126577744bytes",
            "IconIndex": "0",
            "Show Command": "SW_SHOWNORMAL",
            "Drivetype": "DRIVE_FIXED",
            "Driveserialnumber": "ef31ee58",
            "Volumelable": "",
            "Localbasepath Unicode": "None",
            "Localbasepath": "C:\\Users\\User\\AppData\\Local\\Programs\\Notion\\Notion.exe",
            .
            .
            .
        }
    ]
]

It's stored in brackets in brackets in this way. Usually

Isn't it saved like this? I want to know how to save it like that.

The information in json is

        info_list = []
        info = dict()
        file_attribute = self.__file_attribute()

        Contents that will go into #info_list

        info_list.append(info)

It's made like this.

Even after thinking about it for a few days, I can't find an answer. Help me.

python json

2022-09-20 16:21

2 Answers

If you want to include only the dict object, you should include the dict object, not the list.

 json_data = [] # Modify this part

    for f_idx in range(len(file_list)):
        try:
            #save json data
            file = Lnk.file_open(file_list[f_idx])
            file.show_all_info()
            json_data.append(file.get_all_info()) # or modify this part

            print("Save Complete\n\n")

        except:
                print("\n")

    with open('lnk.json', 'w') as json_file:
        json.dump(json_data, json_file, indent=4) # or modify this part


2022-09-20 16:21

See what type file.get_all_info() returns.


2022-09-20 16:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.