Learn Python The Hard Way学习(15) - 读取文件
2012-06-19 14:00
681 查看
学习raw_input和argv是学习读取文件的前提,你可能不能完全理解这个练习,所以认真学习并检查。如果不认真的话,很容易删除一些有用的文件。
这个练习包含两个文件,一个是运行文件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.
我们要做的就是打开这个文件,然后打印文件内容,我们不在代码中写死文件名称,因为我们如果要读取其他文件的话,就要重新修改代码,解决这个问题的办法就是使用argv和raw_input。
上面的代码做了一些有意思的事情,让我们快速的分解一下:
1-3行使用argv取得文件名。第5行使用open命令,现在使用pydoc open看看这个命令的介绍。
第7行打印一行信息,但是第8行有一些新的东西。我们在txt上调用了一个方法。我们通过open方法得到一个file,这个file有一些我们可以调用的方法。使用这些方法的方法就是在file后面加一个.(点),比如txt.read(),就像是说:“嘿,执行读取命令,没有任何参数!”
剩下部分大家在加分练习中分析吧。
运行结果
root@he-desktop:~/mystuff# 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. 给每行添加注释。
2. 学会使用关键词“python THING”搜索python函数的用法。比如“python open”。
3. 这里使用commands这个词,不过经常用的是functions或者methods。网上搜索一下别人是怎么定义的。不要担心被混淆,被别人渊博的知识混淆是很正常的。
4. 删除10-15行,运行代码看看。
5. 只使用raw_input实现上面的代码,想想哪种方法更好,为什么?
6. 运行pydoc file,翻页找到read方法,看到很多其他方法吧,试试看其他的方法。跳过有__的方法,因为这些方法已经被废弃了。
7. 运行python,使用open命令打开一个文件。
>>> txt = open("ex15_sample.txt")
>>> txt.read()
'This is stuff I typed into a file.\nIt is really cool stuff.\nLots and lots of fun to have in here.\n'
8. 让你的txt和txt_again执行以下close()方法,使用完文件后关闭文件是很重要的。
这个练习包含两个文件,一个是运行文件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.
我们要做的就是打开这个文件,然后打印文件内容,我们不在代码中写死文件名称,因为我们如果要读取其他文件的话,就要重新修改代码,解决这个问题的办法就是使用argv和raw_input。
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 = raw_input("> ") txt_again = open(file_again) print txt_again.read()
上面的代码做了一些有意思的事情,让我们快速的分解一下:
1-3行使用argv取得文件名。第5行使用open命令,现在使用pydoc open看看这个命令的介绍。
第7行打印一行信息,但是第8行有一些新的东西。我们在txt上调用了一个方法。我们通过open方法得到一个file,这个file有一些我们可以调用的方法。使用这些方法的方法就是在file后面加一个.(点),比如txt.read(),就像是说:“嘿,执行读取命令,没有任何参数!”
剩下部分大家在加分练习中分析吧。
运行结果
root@he-desktop:~/mystuff# 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. 给每行添加注释。
2. 学会使用关键词“python THING”搜索python函数的用法。比如“python open”。
3. 这里使用commands这个词,不过经常用的是functions或者methods。网上搜索一下别人是怎么定义的。不要担心被混淆,被别人渊博的知识混淆是很正常的。
4. 删除10-15行,运行代码看看。
5. 只使用raw_input实现上面的代码,想想哪种方法更好,为什么?
6. 运行pydoc file,翻页找到read方法,看到很多其他方法吧,试试看其他的方法。跳过有__的方法,因为这些方法已经被废弃了。
7. 运行python,使用open命令打开一个文件。
>>> txt = open("ex15_sample.txt")
>>> txt.read()
'This is stuff I typed into a file.\nIt is really cool stuff.\nLots and lots of fun to have in here.\n'
8. 让你的txt和txt_again执行以下close()方法,使用完文件后关闭文件是很重要的。
相关文章推荐
- 【Python】Learn Python the hard way, ex15 读取文件
- Learn Python The Hard Way学习(16) - 读写文件
- Learn Python The Hard Way学习(16) - 读写文件
- Learn Python The Hard Way学习(17) - 更多的文件操作
- Learn Python The Hard Way学习(20) - 函数和文件
- 【Python】Learn Python the hard way, ex17 文件复制
- Learn Python The Hard Way 15
- Learn Python The Hard Way学习(21) - 函数可以返回信息
- 函数与文件-习题20--learn python the hard way
- 学习python 好书Learn Python The Hard Way
- Learn Python The Hard Way学习(30) - Else和If
- Learn Python The Hard Way学习(31) - 做出选择
- 读写文件,python. learn python the hard way
- Learn Python The Hard Way学习(22) - 到现在我们已经学到了什么?
- Learn Python The Hard Way学习(32) - 循环和列表
- 【在路上】Learn Python The Hard Way 习题心得(ex11,12,13,14,15)
- Learn Python The Hard Way学习(3) - 数字和数学计算
- Learn Python The Hard Way学习(11) - 提问
- Learn Python The Hard Way学习(44) - 继承还是组成?
- Learn Python The Hard Way学习(23) - 读一些代码