Python: user input and commandline arguments
我如何拥有一个可以接受用户输入的Python脚本(假设这是可能的),并且如果从命令行运行,如何让它读入参数?
 To read user input you can try the cmd module for easily creating a mini-command line interpreter (with help texts and autocompletion) and raw_input ( input for Python 3+) for reading a line of text from the user.  
text = raw_input("prompt")  # Python 2
text = input("prompt")  # Python 3
 Command line inputs are in sys.argv .  Try this in your script:  
import sys
print (sys.argv)
 There are two modules for parsing command line options: optparse (deprecated since Python 2.7, use argparse instead) and getopt .  If you just want to input files to your script, behold the power of fileinput .  
The Python library reference is your friend.
var = raw_input("Please enter something: ")
print "you entered", var
或者对于Python 3:
var = input("Please enter something: ")
print("You entered " + str(var))
 raw_input is no longer available in Python 3.x.  But raw_input was renamed input , so the same functionality exists.  
input_var = input("Enter something: ")
print ("you entered " + input_var) 
Documentation of the change
链接地址: http://www.djcxy.com/p/42308.html下一篇: Python:用户输入和命令行参数
