$ 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.
$
加分习题
这节的难度跨越有点大,所以你要尽量做好这节加分习题,然后再继续后面的章节。
1.在每一行的上面用注解说明这一行的用途。
ex15-1.py
form sys import argv #import argument varible from function sys
script, filename = argv #argument variable,include the prameters you transfer to python
txt = open(filename) #define txt as open(filename)
print "Here's your file %r:" % filename #print something
print txt.read() # print the txt's contents
print "Type the filename again:" # print someting
file_again = raw_input("> ") #define file_again,set it as the you type in
txt_again = open(file_again) # set txt_again as open(file_again)
print txt_again.read() # print the txt_again's content
2.如果你不确定答案,就问别人,或者上网搜索。大部分时候,只要搜索 “python” 加上你要搜的东西就能得到你要的答案。比如搜索一下“python open”。
3.我使用了“命令”这个词,不过实际上它们的名字是“函数(function)”和“方法(method)。上网搜索一下这两者的意义和区别。看不明白也没关系,迷失在别的程序员的知识海洋里是很正常的一件事情。
4.删掉 10-15 行使用到 raw_input 的部分,再运行一遍脚本。
ex15-4.py
from sys import argv
script, filename = argv
txt = open(filename)
print "Here's your file %r:" % filename
print txt.read()
运行该程序:
$python ex15-4.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.
$
5.只是用 raw_input 写这个脚本,想想哪种种得到文件名称的方法更好,以及为什么。
ex15-5.py
filename = raw_input("Please input your file name:")
txt = open(filename)
print txt.read()
执行该脚本,可以打印脚本本身:
$python ex15-5.py
Please input your file name:ex15-5.py
filename = raw_input("Please input your file name:")
txt = open(filename)
print txt.read()
$
raw_input好,有提示。
6.运行 pydoc file 向下滚动直到看见 read() 命令(函数/方法)。看到很多别的命令了吧,你可以找几条试试看。不需要看那些包含 __ (两个下划线)的命令,这些只是垃圾而已。
ex15-6.py
filename = raw_input("Pls input your file name:")
txt = open(filename)
print txt.tell()
print txt.readline()
执行该脚本:
$python ex15-6.py
Pls input your file name:ex15-6.py
0
filename = raw_input("Pls input your file name:")
$
7.再次运行 python 在命令行下使用 open 打开一个文件,这种 open 和 read 的方法也值得你一学。
>>> filename = "ex15-sample.txt"
>>> txt = open(filename)
>>> print txt.read()
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
>>>
或者
>>> txt = open("ex15-sample.txt")
>>> print txt.read()
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
>>>
8.让你的脚本针对 txt and txt_again 变量执行一下 close() ,处理完文件后你需要将其关闭,这是很重要的一点。
ex15-8.py
from sys import argv
script, filename = argv
txt = open(filename)
print txt.read()
txt.close()
print "Pls input your filename again:"
file_again = raw_input(">")
txt_again = open(file_again)
print txt_again.read()
txt_again.close()
运行该程序:
$python ex15-8.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.
Pls input your 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.
$
至此,笨方法学python-ex15-读取文件这一小节的学习结束了。