习题20 函数和文件
来源:互联网 发布:淘宝套现信用卡手续费 编辑:程序博客网 时间:2024/06/10 04:50
from sys import argvscript,input_file = argvdef 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 = 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)
这次好像是练习函数和文件协作。
其中 rewind 的意思是 “重绕,倒回”的意思
现在我们要加上注释了!
#-*-coding:utf-8-*-from sys import argv #不解释了script,input_file = argv #两个参数def print_all(f): #定义print_all,这里的f是一个变量,其实也指的是一个文件,比如下面19行的current_file print f.read() #打印f.read() def rewind(f): #定义rewind f.seek(0) #运行f.seek(0)就回到了文件的开始def print_a_line(line_count,f): #定义print_a_line print line_count ,f.readline() #运行f.readline()就会读取文件的一行current_file = open(input_file) #打开一开始输入的文件,然后放到current_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 #定义当前行为1print_a_line(current_line, current_file) #打印当前文件的当前行current_line = current_line + 1 #当前行+1print_a_line(current_line ,current_file) #打印当前文件的当前行current_line = current_line + 1 #当前行+1print_a_line(current_line ,current_file) #打印当前文件的当前行
其实应该看到的结果是
其实这里给我的心得就是变量什么的和C++不一样,我C++习惯一开始定义好
Python里面好像要用即时就加上...
==========================================================================================
附加练习
1.见上面
4. seek()方法在偏移设定该文件的当前位置。参数是可选的,默认为0。
0代表从文件开头开始算起,1代表从当前位置开始算起,2代表从文件末尾算起。
5.其实跟c++一样?
num+=1 就是 num=num+1 的意思吧
0 0
- 习题20 函数和文件
- 笨方法学Python 习题 20: 函数和文件
- 笨方法学习Python-习题20: 函数和文件
- 笨办法学python习题20 函数和文件
- Exercise 20: 函数和文件
- exercise 20 函数和文件
- 练习20——函数和文件
- 笨办法20函数和文件
- python20 函数和文件
- 函数和文件处理
- 临时文件和文件操纵函数
- Learn Python The Hard Way学习(20) - 函数和文件
- 练习20:函数和文件操作的基本方法
- 第20个python程序:函数和文件
- php循环删除目录和文件函数
- loadrunner中目录和文件操作函数
- C常用字符串和文件处理函数
- 内存映射和文件操作函数
- 浮点相等的精度问题
- SSH框架整合简介
- Centos 7.2 配置防火墙
- HashMap的实现原理
- 杨辉三角(利用二维数组)
- 习题20 函数和文件
- Xcode7 编译出错---does not contain bitcode
- Android Bluetooth开发总结-----/*自己编写*/
- ftl判断是否相等,判断是否为空,得到list长度的问题、截取字符串长度
- 【工具技巧】——EA生成架构
- 为什么使用单例模式
- getRequestURI,getRequestURL的区别
- Makefile中用宏定义进行条件编译
- day_14-acm 对拍