简体   繁体   中英

Implement json in python Centos8

I have a.py script to modify a json file. However what I'm doing is loading a json file and then modify it with my code. What I have is the next ñ:


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

I'm trying something like this but I got the next error in 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

Is there any solution to automatize the json file? So when I'm using the command python3 main.py example.py helloWorld.json to generate a modify.json from that helloWorld.json file

I'm using Centos8 and the path of the files are /root

Thanks in advance!

your str(sys.argv[1:]) is "['helloWorld.json']" which is not file. try using:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM