习题15
python:3.9

from sys import argv

script,filename = argv

txt = open (filename)

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())

结果如下

PS C:\Users\78523\mybuff> python ex15.py ex15_sample.txt
Here's your file 'ex15_sample.txt' :
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.

Type the filenanme again:>ex15_sample.txt
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.

加分习题这节的难度跨越有点大,所以你要尽量做好这节加分习题,然后再继续后面的章节。
1.在每一行的上面用注解说明这一行的用途

#从sys中使用argv列表
from sys import argv
#脚本本身名字,传入脚本的参数
script, filename =argv
#打开filename这个参数所表示的文档,并将这个file传给txt
txt = open(filename)
#打印字符串,并代入文件名
print ("Here's your file %r :"%filename)
#打印字符串,并读取文件
print (txt.read())
#打印字串符,
print ("Type the filenanme again:")
#定义file,并使用input输入
file_again = input(">")
#定义txt_again,并打开file传给txt
txt_again =open (file_again)
#打印字符串,并读取文件
print (txt_again.read())

这个答案可能不是特别标准,大家多查一下
2.如果你不确定答案,就问别人,或者上网搜索。大部分时候,只要搜索“python” 加上你要搜的东西就能得到你要的答案。比如搜索一下“python open”。
这里我是用了输入 python -m pydoc open,查看,英文过于复杂,没明白意思,建议大家上网查找
3.我使用了“命令”这个词,不过实际上它们的名字是“函数(function)”和“方法(method)。上网搜索一下这两者的意义和区别。看不明白也没关系,迷失在别的程序员的知识海洋里是很正常的一件事情。
4.删掉10-15 行使用到 raw_input的部分,再运行一遍脚本。

#从sys中使用argv列表
from sys import argv
#脚本本身名字,传入脚本的参数
script, filename =argv
#打开filename这个参数所表示的文档,并将这个file传给txt
txt = open(filename)
#打印字符串,并代入文件名
print ("Here's your file %r :"%filename)
#打印字符串,并读取文件
print (txt.read())
#打印字串符,
print ("Type the filenanme again:")
#定义file,并使用input输入
file_again = input(">")
#定义txt_again,并打开file传给txt
txt_again =open (file_again)
#打印字符串,并读取文件
print (txt_again.read())

运行结果

PS C:\Users\78523\mybuff> python ex15.py ex15_sample.txt
Here's your file 'ex15_sample.txt' :
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.

Type the filenanme again:

再次运行没有了再次输入文件名的过程,也不会再次运行文件

5.只是用 raw_input写这个脚本,想想那种得到文件名称的方法更好,以及为什么。
这里我自己尝试了一下,直接看过程吧

file = input(">")

txt = open (file)

print (txt. read())

运行结果

PS C:\Users\78523\mybuff> python ex15.py
>ex15_sample.txt
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.

个人感觉input这种方法更方便一些,只用简单几行就能到结果

6 .运行 pydoc file向下滚动直到看见read()命令(函数/方法)。看到很多别的命令了吧,你可以找几条试试看。不需要看那些包含__(两个下划线)的命令,这些只是垃圾而已。

7.再次运行 python在命令行下使用 open打开一个文件,这种 open 和 read 的方法也值得你一学。
8.让你的脚本针对 txt and txt_again变量执行一下 close(),处理完文件后你需要将其关闭,这是很重要的一点。

from sys import argv

script,filename = argv

txt = open (filename)

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())

运行结果

PS C:\Users\78523\mybuff> python ex15.py ex15_sample.TXT
Here's your file 'ex15_sample.TXT':
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.

Type the filename again:
> ex15_sample.txt
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.

最后提醒自己一下6,7题没研究明白,等以后解答