笨方法学Python 习题 20: 函数和文件
2017-07-28 16:49
218 查看
#!usr/bin/python
# -*-coding:utf-8-*-
from sys import argv
script , input_file = argv
def print_all(f):
print (f.read())
#新建print_all函数 定义print (f.read())为函数代码
def rewind(f):
f.seek(0)
#新建rewind函数 定义f.seek(0)为函数代码
def print_a_line(line_count,f):
print (line_count , f.readline())
#新建print_a_line函数 定义print (line_count , f.readline())为函数代码
current_file = open(input_file)
#定义变量current_file 打开txt文档
print ("First let's print the whole file:\n")
#打印First let's print the whole file:
print_all(current_file)
#打印读取后的txt文档内容
print ("Now let's rewind, kind of like a tape.")
#打印Now let's rewind, kind of like a tape.
rewind(current_file)
#定位当前位置
print ("Let's print three lines:")
#打印Let's print three lines:
current_line = 1
print_a_line(current_line,current_file)
#打印文档第一排内容
current_line = current_line + 1
print_a_line(current_line,current_file)
#打印文档第二排内容
current_line = current_line + 1
print_a_line(current_line,current_file)
#打印文档第三排内容
运行结果如下:
$ python ex20.py test.txt
First let's print the whole file:
To all the people out there.
I say I don't like my hair.
I need to shave it off.
Now let's rewind, kind of like a tape.
Let's print three lines:
1 To all the people out there.
2 I say I don't like my hair.
3 I need to shave it off.
$
加分习题
①通读脚本,在每行之前加上注解,以理解脚本里发生的事情。
②每次 print_a_line 运行时,你都传递了一个叫 current_line 的变量。在每次调用函数时,打印出 current_line 的至,跟踪一下它在 print_a_line 中是怎样变成 line_count 的。
③找出脚本中每一个用到函数的地方。检查 def 一行,确认参数没有用错。
④上网研究一下 file 中的 seek 函数是做什么用的。试着运行 pydoc file 看看能不能学到更多。
seek()方法在偏移设定该文件的当前位置。参数是可选的,默认为0,这意味着绝对的文件定位,它的值如果是1,这意味着寻求相对于当前位置,2表示相对于文件的末尾。
seek()方法的语法:1fileObject.seek(offset[, whence])
参数
offset -- 这是在文件中,读/写指针的位置。
whence -- 这是可选的,默认为0,这意味着绝对的文件定位,其它的值是1,这意味着寻求相对于当前位置,2表示相对于文件的末尾。
fo = open("foo20.txt", "r")
print ("Name of the file: ", fo.name)
# Assuming file has following 5 lines
# This is 1st line
# This is 2nd line
# This is 3rd line
# This is 4th line
# This is 5th line
line = fo.readline()
print ("Read Line: %s" % (line))
# Again set the pointer to the beginning
fo.seek(0, 0)
line = fo.readline()
print ("Read Line: %s" % (line))
# Close opend file
fo.close()
运行结果如下:
Name of the file: foo.txt
Read Line: Assuming file has following 5 lines
Read Line: Assuming file has following 5 lines
⑤研究一下 += 这个简写操作符的作用,写一个脚本,把这个操作符用在里边试一下。
常见问题回答
print_all 和其它函数里的 f 是什么?
和 Ex 18 里的一样, f 只是一个变量名而已,不过在这里它指的是一个文件。Python 里的文件就和老式磁带机,或者 DVD 播放机差不多。它有一个用来读取数据的“磁头”,你可以通过这个“磁头”来操作文件。每次你运行 f.seek(0) 你就回到了文件的开始,而运行 f.readline() 则会读取文件的一行,然后将“磁头”移动到 \n 后面。后面你会看到更详细的解释。
问什么文件里会有间隔空行?
readline() 函数返回的内容中包含文件本来就有的 \n,而 print 在打印时又会添加一个 \n,这样一来就会多出一个空行了。解决方法是在 print 语句结尾加一个逗号 ,,这样 print 就不会把它自己的 \n 打印出来了。
python 3x,在结尾加上,end =""。
为什么 seek(0) 没有把 current_line 设为 0?
首先 seek() 函数的处理对象是 字节 而非行,所以 seek(0) 只是转到文件的 0 byte,也就是第一个 byte 的位置。其次, current_line 只是一个独立变量,和文件本身没有任何关系,我们只能手动为其增值。
+= 是什么?
英语里边 “it is” 可以写成 “it’s”,”you are” 可以写成 “you’re”,这叫做简写。而这个操作符是吧 = 和 + 简写到一起了。 x += y 的意思和 x = x + y 是一样的。
readline() 是怎么知道每一行在哪里的?
readline() 里边的代码会扫描文件的每一个字节,直到找到一个 \n 为止,然后它停止读取文件,并且返回此前的文件内容。文件 f 会记录每次调用 readline() 后的读取位置,这样它就可以在下次被调用时读取接下来的一行了。
# -*-coding:utf-8-*-
from sys import argv
script , input_file = argv
def print_all(f):
print (f.read())
#新建print_all函数 定义print (f.read())为函数代码
def rewind(f):
f.seek(0)
#新建rewind函数 定义f.seek(0)为函数代码
def print_a_line(line_count,f):
print (line_count , f.readline())
#新建print_a_line函数 定义print (line_count , f.readline())为函数代码
current_file = open(input_file)
#定义变量current_file 打开txt文档
print ("First let's print the whole file:\n")
#打印First let's print the whole file:
print_all(current_file)
#打印读取后的txt文档内容
print ("Now let's rewind, kind of like a tape.")
#打印Now let's rewind, kind of like a tape.
rewind(current_file)
#定位当前位置
print ("Let's print three lines:")
#打印Let's print three lines:
current_line = 1
print_a_line(current_line,current_file)
#打印文档第一排内容
current_line = current_line + 1
print_a_line(current_line,current_file)
#打印文档第二排内容
current_line = current_line + 1
print_a_line(current_line,current_file)
#打印文档第三排内容
运行结果如下:
$ python ex20.py test.txt
First let's print the whole file:
To all the people out there.
I say I don't like my hair.
I need to shave it off.
Now let's rewind, kind of like a tape.
Let's print three lines:
1 To all the people out there.
2 I say I don't like my hair.
3 I need to shave it off.
$
加分习题
①通读脚本,在每行之前加上注解,以理解脚本里发生的事情。
②每次 print_a_line 运行时,你都传递了一个叫 current_line 的变量。在每次调用函数时,打印出 current_line 的至,跟踪一下它在 print_a_line 中是怎样变成 line_count 的。
③找出脚本中每一个用到函数的地方。检查 def 一行,确认参数没有用错。
④上网研究一下 file 中的 seek 函数是做什么用的。试着运行 pydoc file 看看能不能学到更多。
seek()方法在偏移设定该文件的当前位置。参数是可选的,默认为0,这意味着绝对的文件定位,它的值如果是1,这意味着寻求相对于当前位置,2表示相对于文件的末尾。
seek()方法的语法:1fileObject.seek(offset[, whence])
参数
offset -- 这是在文件中,读/写指针的位置。
whence -- 这是可选的,默认为0,这意味着绝对的文件定位,其它的值是1,这意味着寻求相对于当前位置,2表示相对于文件的末尾。
fo = open("foo20.txt", "r")
print ("Name of the file: ", fo.name)
# Assuming file has following 5 lines
# This is 1st line
# This is 2nd line
# This is 3rd line
# This is 4th line
# This is 5th line
line = fo.readline()
print ("Read Line: %s" % (line))
# Again set the pointer to the beginning
fo.seek(0, 0)
line = fo.readline()
print ("Read Line: %s" % (line))
# Close opend file
fo.close()
运行结果如下:
Name of the file: foo.txt
Read Line: Assuming file has following 5 lines
Read Line: Assuming file has following 5 lines
⑤研究一下 += 这个简写操作符的作用,写一个脚本,把这个操作符用在里边试一下。
常见问题回答
print_all 和其它函数里的 f 是什么?
和 Ex 18 里的一样, f 只是一个变量名而已,不过在这里它指的是一个文件。Python 里的文件就和老式磁带机,或者 DVD 播放机差不多。它有一个用来读取数据的“磁头”,你可以通过这个“磁头”来操作文件。每次你运行 f.seek(0) 你就回到了文件的开始,而运行 f.readline() 则会读取文件的一行,然后将“磁头”移动到 \n 后面。后面你会看到更详细的解释。
问什么文件里会有间隔空行?
readline() 函数返回的内容中包含文件本来就有的 \n,而 print 在打印时又会添加一个 \n,这样一来就会多出一个空行了。解决方法是在 print 语句结尾加一个逗号 ,,这样 print 就不会把它自己的 \n 打印出来了。
python 3x,在结尾加上,end =""。
为什么 seek(0) 没有把 current_line 设为 0?
首先 seek() 函数的处理对象是 字节 而非行,所以 seek(0) 只是转到文件的 0 byte,也就是第一个 byte 的位置。其次, current_line 只是一个独立变量,和文件本身没有任何关系,我们只能手动为其增值。
+= 是什么?
英语里边 “it is” 可以写成 “it’s”,”you are” 可以写成 “you’re”,这叫做简写。而这个操作符是吧 = 和 + 简写到一起了。 x += y 的意思和 x = x + y 是一样的。
readline() 是怎么知道每一行在哪里的?
readline() 里边的代码会扫描文件的每一个字节,直到找到一个 \n 为止,然后它停止读取文件,并且返回此前的文件内容。文件 f 会记录每次调用 readline() 后的读取位置,这样它就可以在下次被调用时读取接下来的一行了。
相关文章推荐
- 笨方法学习Python-习题20: 函数和文件
- python 笔记 函数和文件(seek和readline)《笨办法学Python》习题20 ——12.28
- 函数与文件-习题20--learn python the hard way
- 笨办法学python习题20 函数和文件
- 本方法学python 习题24(综合练习) 习题25(更多关于函数和变量的练习)习题26(修改程序)
- 练习20:函数和文件操作的基本方法
- 习题20 函数和文件
- 笨方法学Python 习题 18: 命名、变量、代码、函数
- 习题35,分支和函数,笨方法学python
- 笨方法学习Python-习题17: 更多文件操作
- 笨方法学Python 习题 16: 读写文件
- Python中在脚本中引用其他文件函数的实现方法
- 笨方法学习Python-习题18: 命名、变量、代码、函数
- Learn Python The Hard Way学习(20) - 函数和文件
- 笨方法学python16习题 【读写文件】 最详解
- 笨方法学Python 习题 19: 函数和变量
- Python os.path 关于关于文件/目录的常用函数和方法
- python基础:os模块中关于文件/目录常用的函数使用方法
- 笨方法学Python 习题 21: 函数可以返回东西
- Python中在脚本中引用其他文件函数的实现方法