我需要编写一个脚本,将所有参数添加到 Python 列表中,然后将它们保存到文件中

I need to write a script that adds all arguments to a Python list, and then save them to a file

出于某种原因,文件保存为“列表列表”

#!/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

args[1:] 是一个切片 - 它 returns 一个列表,您可以将其附加到列表中,从而形成一个列表列表。

也许你想要my_list = args[1:]?否则,您可以使用 my_list.extend(args[1:])