1、命令行输入:
import sys
name = sys.argv[0]
digits = sys.argv[1]
test = sys.argv[2]
print (name,digits,test)
2、从键盘输入:
line = input()
例:从根据提示从键盘输入行和列,并打印出来
def get_int(msg, minimum, default):
while True:
try:
line = input(msg)
if not line and default is not None:
return default
i = int(line)
if i < minimum:
print("must be >=", minimum)
else:
return i
except ValueError as err:
print(err)
def main():
rows = get_int("input rows: ", 1, None)
columns = get_int("input columns: ", 1, None)
print("rows = ", rows)
print("columns = ", columns)
main()
3、文件读取
def openfile():
all_the_text = ""
file_object = open('Ascii.py')<span style="white-space:pre"> </span>#打开当前文件夹内的文件
try:
all_the_text = file_object.read( )
finally:
file_object.close( )
print(all_the_text)