Need to pass argument while accessing python json key

I need to access json file using command line argument

my code is python.py:

#########################################
import json
import sys
from sys import argv
from pprint import pprint

with open('cdh/cdh5.json') as data_file:
    data = json.load(data_file)

pprint(data['projects']['abcd']['track-branch'])

#################################################

so , instead of project abcd , i need to pass abcd from command line while executing this python script.

eg1: python.py abcd

will do : pprint(data['projects']['abcd']['track-branch'])

eg2: python.py efgh

will do : pprint(data['projects']['efgh']['track-branch'])

i tried doing this:-

pprint(data['projects']['sys.args']['track-branch']) but it gives me below error :

pprint(data['projects']['sys.argv']['track-branch'])
KeyError: 'sys.argv'


Source: bash

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.