在 python Centos8 中实施 json

Implement json in python Centos8

我有一个 .py 脚本来修改 json 文件。然而,我正在做的是加载一个 json 文件,然后用我的代码修改它。我有的是下一个 ñ:


 with open('example.json', 'r+') as file:
    dictionary_data = json.load(file)
    .  
    .(code)
    .
    .
    .
 new_file = open("modify.json", "w") 

我正在尝试类似的操作,但我在 CentOs8 中遇到了下一个错误

with open(str(sys.argv[1:]), 'r+') as file:
  dictionary_data = json.load(file)
Traceback (most recent call last):
  File "main.py", line 12, in <module>
    with open(str(sys.argv[1:]), 'r+') as file:
    FileNotFoundError: [Errno 2] No such file or directory: "['helloWorld.json']"
The HelloWorld file is in the same directory as the main.py

有什么解决方案可以使 json 文件自动化吗?因此,当我使用命令 python3 main.py example.py helloWorld.json 从 helloWorld.json 文件

生成 modify.json

我用的是Centos8,文件路径是/root

提前致谢!

您的 str(sys.argv[1:])"['helloWorld.json']",这不是文件。 尝试使用:

with open(str(sys.argv[1]), 'r+') as file: # for example.json

with open(str(sys.argv[2]), 'r+') as file: # for helloworld.json