Python · OS, sys, argparse & subprocess

In parser.add_argument("-o", "--operation", default="add"), what role does each of the three parts play?

Answer locked. Get the free KnowCard app to reveal it — plus spaced-repetition review so it actually sticks.

Get it on App StoreGet it on Google Play

This is one card from the KnowCard library — thousands more across SAP, Linux, Python and more. In the app you get the answer, AI explanations, and cards that come back right before you would forget them. Free to start on iOS, Android or the web.

More in OS, sys, argparse & subprocess

You run an external tool with subprocess.run(["mytool", "--check"]). It fails (exit code 1) but your Python script keeps going as if nothing happened. How do you make the failure raise?
What does `r.stdout` hold after this, and why is `text=True` important? ``` r = subprocess.run( ["echo", "hi"], capture_output=True, text=True) ```
What are sys.stdin, sys.stdout, and sys.stderr, and why send error/diagnostic messages to stderr rather than stdout?
You read a config value with os.environ["API_KEY"]. It works on your machine but crashes with KeyError on a fresh deploy where the var isn't set. What's the safe way to read it?
What does this print when run as `python prog.py hello`? ``` import sys print(len(sys.argv)) print(sys.argv[0]) ```
A script is called `python add.py 2 3` with this body. What does it print? ``` import sys print(sys.argv[1] + sys.argv[2]) ```

Start learning today

Free to start — download the app or use it in your browser.

Get it on App StoreGet it on Google Play