1.argv详解
#argv的含义是把argv中的东西解包,
#将所有的参数依次赋值给左边的这些变量。#命令行里的参数1,2,3可以任意写,代码中的one, two, third会自动识别
2.open的一些用法
#在cmd或者powershell输出命令,而不是在IDLE里面。
#命令格式 python py文件地址 txt文件地址
from sys import argv
#sys是一个软件包,意思是从sys这个软件包里取出argv这个模块来供我使用
script, filename = argv
txt = open(filename)
#把 open(filename)返回的值 赋给变量txt,可以简化下面的代码
print("Here's your file %r:" % filename)
print(txt.read())
print("Type the filename again:")
file_again = input('>')
txt_again = open(file_again)
print(txt_again.read())
3.zork小游戏
from sys import argv
script, user_name,my_lord = argv
prompt = '>'
print("Hi %s, I'm the %s script,%s." % (user_name, script,my_lord))
print("I'd like to ask you a few questions,%s." % my_lord)
print("Do you like me %s,%s?" % (user_name,my_lord))
likes = input(prompt)
print("Where do you live %s,%s?" % (user_name,my_lord))
lives = input(prompt)
print("What kind of computer do you have,%s?" % my_lord)
computer = input(prompt)
print(
"Alright,%s .so you said %r about liking me."
"you live in %r. Not sure where that is."
"And you have a %r computer. Nice."
% (my_lord,likes, lives, computer)
)