How do I have a Python script that can accept user input and how do I make it read in arguments if run from the command line?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To read user input you can try the
cmdmodule for easily creating a mini-command line interpreter (with help texts and autocompletion) andraw_input(inputfor Python 3+) for reading a line of text from the user.Command line inputs are in
sys.argv. Try this in your script:There are two modules for parsing command line options:
(deprecated since Python 2.7, useoptparseargparseinstead) andgetopt. If you just want to input files to your script, behold the power offileinput.The Python library reference is your friend.