0

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:

if name=='main':

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

new_file = open("modify.json", "w")

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

Luka
  • 115
  • 7
  • I think you are looking for this https://www.tutorialspoint.com/python/python_command_line_arguments.htm Adding the parameter to your file would change it into something like this ``` import sys with open(sys.argv[1], 'r+') as file: dictionary_data = json.load(file) . .(code) . . . ``` – Sytze Andringa Dec 11 '20 at 09:01
  • I wanted to reply as an awnser but it got closed earlier – Sytze Andringa Dec 11 '20 at 09:01
  • @Gaargod I'm trying that solution but it got wrong when I execute in bash with that command ```python3 main.py helloWorld.json```. However if I load the file as I explained above It's generating the json file that a I'm looking for. Any aditional idea? – Luka Dec 11 '20 at 09:12
  • I get why it is closed. But since I already typed it out I decided to still comment it. I just want to help. @Luka maybe try conversion to string, by using `string(sys.argv[1])` – Sytze Andringa Dec 11 '20 at 09:15
  • @Gaargod Sorry to bother you again but I'm reading many post about this issue and I couldn't resolve it. When i execute the command ```python3 main.py constants.py example.json ``` ```FileNotFoundError: [Errno 2] No such file or directory: "['constants.py', 'example.json']" ``` Any idea? I have both files in the same directory as main.py – Luka Dec 11 '20 at 11:52

0 Answers0