Home > Back-end >  I need to write a script that adds all arguments to a Python list, and then save them to a file
I need to write a script that adds all arguments to a Python list, and then save them to a file

Time:06-02

for some reason the file is saving as a "list of lists"

#!/usr/bin/python3
import sys
import json
save_to_json_file = __import__('5-save_to_json_file').save_to_json_file
load_from_json_file = __import__('6-load_from_json_file').load_from_json_file

args = sys.argv
filename = "add_item.json"
with open(filename, 'a ', encoding="utf-8") as f:
    my_list = []
    my_list.append(args[1:])
    save_to_json_file(my_list, filename)
    load_from_json_file(filename)

this is the file that is creating

CodePudding user response:

args[1:] is a slice - it returns a list, which you append to a list, making a list of lists.

Perhaps you want my_list = args[1:]? Otherwise, you can use my_list.extend(args[1:])

  • Related