0

I'm creating this automation script in python that uses a dictionary to open a browser and when using webbrowser module to open a parsed dictionary, I keep getting a key error. Here is the code:

import webbrowser, sys, requests
ptcl = 'https://'
tail = '.com/'
context_dict =
    {
        'fb': ptcl + 'facebook' + tail,
        'ig': ptcl + 'instagram' + tail,
        'google': ptcl + 'google' + tail,
        'kat': ptcl + 'kat.cr',
        'mail': ptcl + 'gmail' + tail,
        'utube': ptcl + 'youtube' + tail,       
        }


def open_page(page):
    webbrowser.open(context_dict[page])
def get_args():
    if len(sys.argv) > 1:
        for i in sys.argv[1:]:
            page = context_dict[i.replace(',', '')]
            open_page(page)

if __name__ == '__main__':
    get_args()

Here is an example of when trying to access fb from the cmd sample fb run

1 Answer 1

1

You lookup twice:

page = context_dict[i.replace(',', '')]

then

webbrowser.open(context_dict[page])

Remove one of the lookups.

I would suggest:

def get_args():
    if len(sys.argv) > 1:
        for i in sys.argv[1:]:
            key = i.replace(',', '')
            open_page(key)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I was redundant

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.