There is a python script, I need to run it with a key.

Example: python script.py --path путь

How in the code to track what the user entered?

  • Use the log. Or, use debug on some IDE - hedgehogues
  • @hedgehogues It is possible in more detail - Log1c0

2 answers 2

Use the argparse package. With it, you can add flags with which to run your application.

 import argparse parser = argparse.ArgumentParser(description="Example of a single flag acting as a boolean and an option.") parser.add_argument('--foo', nargs='?', const="bar", default=False) parser.add_argument('--woo') args = parser.parse_args() if args.foo: print(args.foo) else: print("Using the default, boolean False.") if args.woo: print(args.woo) else: print("Using the default, boolean False.") 
  • And how to display an error if there are no arguments? - Log1c0
  • Use try - hedgehogues

You can use sys.argv You must first import the sys library, and then refer to the desired element of the sys.argv array sys.argv

Here is an example:

 import sys if '--path' in sys.argv: print sys.argv[sys.argv.index('--path') + 1] 
  • one
    Why use sys when there is a ready-made package that produces more or less convenient parsing of arguments? - hedgehogues