Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add "are you sure? (y/n)" prompt before executing python script

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.

like image 414
Matt C. Avatar asked Mar 03 '17 00:03

Matt C.


People also ask

How do I execute a Python script?

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!

How do you ask a yes or no question in Python?

answer = input("Enter yes or no: ") if answer == "yes": # Do this. elif answer == "no": # Do that. else: print("Please enter yes or no.")

How do you pass command prompt in Python?

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.

How do you add to a Python script?

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”.


2 Answers

You could put something like this at the start of your script

if input("are you sure? (y/n)") != "y":
    exit()
like image 79
Mark Omo Avatar answered Oct 29 '22 16:10

Mark Omo


raw_input() was renamed to input() in Python 3.0.

like image 26
Rui Martins Avatar answered Oct 29 '22 15:10

Rui Martins