sys.argv[1]
and/or sys.argv[2]
will never be None. In case it is missing, you will get IndexError the moment when you try to access element with non-existing index in sys.argv. You may look at click for creating nice cli interface. Or at least at argparse module from standard library.import json import sys import csv import pandas as pd try: _, json_file, csv_file = sys.argv except ValueError: print(f'Incorrect number of CLI arguments.\nUse: yourscript.py json_file csv_file') else: with open(json_file) as f: data = json.load(f) # using csv with open(csv_file, 'w') as f: wrtr = csv.DictWriter(f, fieldnames=data['all-services'][0].keys()) wrtr.writeheader() wrtr.writerows(data['all-services']) # alternative - usnig pandas df = pd.json_normalize(data['all-services']) df.to_csv(csv_file, index=False)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs