Say I have python script, call it "script.py".
Normally, in the command line, file executes when user types "python script.py".
What I want is to add an "are you sure? (y/n)" prompt after user types "python script.py". And only after typing y [enter] should the script execute.
To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World! If everything works okay, after you press Enter , you'll see the phrase Hello World!
answer = input("Enter yes or no: ") if answer == "yes": # Do this. elif answer == "no": # Do that. else: print("Please enter yes or no.")
Method 1 (CMD /K): Execute a command and then remain Now what if you want to execute multiple command prompt commands from Python? If that's the case, you can insert the '&' symbol (or other symbols, such as '&&' for instance) in between the commands.
To add arguments to Python scripts, you will have to use a built-in module named “argparse”. As the name suggests, it parses command line arguments used while launching a Python script or application. These parsed arguments are also checked by the “argparse” module to ensure that they are of proper “type”.
You could put something like this at the start of your script
if input("are you sure? (y/n)") != "y":
exit()
raw_input()
was renamed to input()
in Python 3.0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With