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?
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 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.") 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] sys when there is a ready-made package that produces more or less convenient parsing of arguments? - hedgehoguesSource: https://ru.stackoverflow.com/questions/875753/
All Articles