笨办法学Python(二十)
习题 20: 函数和文件
回忆一下函数的要点,然后一边做这节练习,一边注意一下函数和文件是如何在一起协作发挥作用的。
from sys import argv script, input_file = argv def print_all(f):
print f.read() def rewind(f):
f.seek(0) def print_a_line(line_count, f):
print line_count, f.readline() current_file = open(input_file) print "First let's print the whole file:\n" print_all(current_file) print "Now let's rewind, kind of like a tape." rewind(current_file) print "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)
特别注意一下,每次运行 print_a_line 时,我们是怎样传递当前的行号信息的。
你应该看到的结果
加分习题
- 通读脚本,在每行之前加上注解,以理解脚本里发生的事情。
- 每次 print_a_line 运行时,你都传递了一个叫 current_line 的变量。在每次调用函数时,打印出 current_line 的至,跟踪一下它在 print_a_line 中是怎样变成 line_count 的。
- 找出脚本中每一个用到函数的地方。检查 def 一行,确认参数没有用错。
- 上网研究一下 file 中的 seek 函数是做什么用的。试着运行 pydoc file 看看能不能学到更多。
- 研究一下 += 这个简写操作符的作用,写一个脚本,把这个操作符用在里边试一下。
习题练习
1.
# 从 SYS 模组导入argv
from sys import argv # 把 argv 的列表变量赋值给 script 和 input_file
script, input_file = argv # 定义 print_all 函数
def print_all(f):
# 打印形参内容
print f.read() # 定义 rewind 函数
def rewind(f):
# 函数使用 seek 设置文件指针偏移
f.seek(0) # 定义 print_a_line 函数
def print_a_line(line_count, f):
# readline() 读取一行内容,并将文件内部指针移向下一行
print line_count, f.readline() # 用 open 函数打开文件,并赋值给 current_file 变量
current_file = open(input_file) print "First let's print the whole file:\n" print_all(current_file) print "Now let's rewind, kind of like a tape." rewind(current_file) print "Let's print three lines:" # 行号变量置 1
current_line = 1
print_a_line(current_line, current_file) # 行号变量加 1
current_line = current_line + 1
print_a_line(current_line, current_file) current_line = current_line + 1
print_a_line(current_line, current_file)
2.
readline()方法从文件中读取一整行。尾部的换行符保持在字符串中。
http://www.jb51.net/article/66630.htm
https://zhidao.baidu.com/question/1306305405518632699.html
4.
seek()方法在偏移设定该文件的当前位置。没有返回值。
http://blog.csdn.net/yyywyr/article/details/45508819
http://blog.csdn.net/solo95/article/details/50564575
http://www.jb51.net/article/66631.htm
5.
a += b 即等同于 a = a + b
笨办法学Python(二十)的更多相关文章
- 笨办法学Python(十二)
习题 12:提示别人 当你键入 raw_input() 的时候,你需要键入 ( 和 ) 也就是“括号(parenthesis)”.这和你格式化输出两个以上变量时的情况有点类似,比如说 "%s ...
- 笨办法学Python(十九)
习题 19: 函数和变量 函数这个概念也许承载了太多的信息量,不过别担心.只要坚持做这些练习,对照上个练习中的检查点检查一遍这次的联系,你最终会明白这些内容的. 有一个你可能没有注意到的细节,我们现在 ...
- 笨办法学Python(十八)
习题 18: 命名.变量.代码.函数 标题包含的内容够多的吧?接下来我要教你“函数(function)”了!咚咚锵!说到函数,不一样的人会对它有不一样的理解和使用方法,不过我只会教你现在能用到的最简单 ...
- 笨办法学Python(十五)
习题 15: 读取文件 你已经学过了 raw_input 和 argv,这些是你开始学习读取文件的必备基础.你可能需要多多实验才能明白它的工作原理,所以你要细心做练习,并且仔细检查结果.处理文件需要非 ...
- 笨办法学Python(十)
习题 10: 那是什么? 在习题 9 中我你接触了一些新东西.我让你看到两种让字符串扩展到多行的方法.第一种方法是在月份之间用 \n (back-slash n )隔开.这两个字符的作用是在该位置上放 ...
- 笨办法学Python(十六)
习题 16: 读写文件 如果你做了上一个练习的加分习题,你应该已经了解了各种文件相关的命令(方法/函数).你应该记住的命令如下: close – 关闭文件.跟你编辑器的 文件->保存.. 一个意 ...
- 笨办法学Python(十四)
习题 14:提示和传递 让我们使用 argv 和 raw_input 一起来向用户提一些特别的问题.下一节习题你会学习如何读写文件,这节练习是下节的基础.在这道习题里我们将用略微不同的方法使用 raw ...
- 笨办法学 Python (Learn Python The Hard Way)
最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注 ...
- 笨办法学 Python (第三版)(转载)
笨办法学 Python (第三版) 原文地址:http://blog.sina.com.cn/s/blog_72b8298001019xg8.html 摘自https://learn-python ...
- 笨办法学Python - 习题1: A Good First Program
在windows上安装完Python环境后,开始按照<笨办法学Python>书上介绍的章节进行练习. 习题 1: 第一个程序 第一天主要是介绍了Python中输出函数print的使用方法, ...
随机推荐
- python 函数调用
##########定义函数######### 如果不主动调用函数,函数是不会执行的 def say_hello(): print 'hello1' print 'hello2' ...
- HDFS配额查询
### 查看目录配额 hdfs dfs -count -q -h /user/hive/warehouse/db_name.db ### 查看整个HDFS的空间大小 hdfs dfs -df -h / ...
- 「linux」后台启动nohup经常跟的>/dev/null 2>&1是什么意思
https://www.cnblogs.com/wangsongbai/p/10215155.html 我们在写启动脚本的时候,会用nohup &,这个大家都知道,但是经常会有 >/de ...
- my16_sql_thread执行慢导致主从延迟高的一个情景
现象:从库延迟高,查看slave status发现sql_thread执行语句的速度比主库慢,这样的延迟会一直高下去,下面是排查的一些过程1. 检查了从库的配置,磁盘的写入速度的确没有主库高2. io ...
- mpdf Could not find image file (http://local.com/xxxxx)
记录一下昨天和今天遇到的,yii2使用mpdf的时候,图片是使用php方法生成的二维码,所以图片地址为http://local.com/xxxxx,url中携带不同的参数. 但是开启了 $mpdf-& ...
- ES6数组新增方法总结
关于数组中forEach() .map().filter().reduce().some().every()的总结 let arr = [1, 2, 3, 4, 5] // forEach遍历数组 a ...
- 用navigator.geolocation.getCurrentPosition在IOS10以上的系统无法定位
昨天老板告诉我代码有Bug(定位失败),于是各种测试最终发现IOS10以上版本手机不能成功(穷,买不起iphone,测试不完全),先贴失败代码: var city =""; nav ...
- JavaSE---位运算符
1.Java支持的位运算符有7个: &:按位与 [2个相同取相同.2个不同取0] |:按位或 [2个相同取相同.2个不同取1] ~:按位非 ^:按位异或 [2个相同取0.2个不同取1] < ...
- public class 与 class 的区别
public class 与 class 的区别 1.一个类前面的public是可有可无的 2.如果一个类使用 public 修饰,则文件名必须与类名一致 3.如果一个类前面没有使用public修饰, ...
- awk, sed, xargs, bash
http://ryanstutorials.net/ awk: split($1, arr, “\t”) sed: sed -n '42p' file sed '42d' file sed ' ...