笨方法学Python 习题 20: 函数和文件

来源:互联网 发布:java 读取压缩包 编辑:程序博客网 时间:2024/05/14 14:40

#!usr/bin/python# -*-coding:utf-8-*-from sys import argvscript , input_file = argvdef 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 = 1print_a_line(current_line,current_file)#打印文档第一排内容current_line = current_line + 1print_a_line(current_line,current_file)#打印文档第二排内容current_line = current_line + 1print_a_line(current_line,current_file)#打印文档第三排内容

运行结果如下:

$ python ex20.py test.txtFirst 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 beginningfo.seek(0, 0)line = fo.readline()print ("Read Line: %s" % (line))# Close opend filefo.close()

运行结果如下:

Name of the file: foo.txtRead 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() 后的读取位置,这样它就可以在下次被调用时读取接下来的一行了。

原创粉丝点击