1.File.readline() 方法用于从文件读取整行,每次读取一行,如此循环,包括 “\n” 字符。如果指定了一个非负数的参数,则返回指定大小的字节数,包括 “\n” 字符。
readline解析
read/readline/readlines详解

2.File.seek()方法用于移动文件读取指针到指定位置 seek方法

ex20_sample.txt内容如下

To all the people out there.
I say I don’t like my hair.
I need to shave it off.

脚本内容

from sys import argv
scripts,input_file = argv           #这里的input_file为ex20_sample.txt,一共三行内容
current_file = open(input_file)     #把input_file打开,返回一个文件对象给current_file
current_line = 1          #定义当前行为1def print_all(f):    #一个参数f,函数print_all可以将参数f的内容read后输出print(f.read())    def rewind(f):       #一个参数f,函数rewind可以将参数f的内容f.seek(0)        #将指针移动到最开始的位置,也即0def print_a_line(line_count,f):   #两个参数line_count和f,第一个参数直接输出,第二个参数使用readline进行输出print(line_count,f.readline())   print("First let's print the whole file:")
print_all(current_file)   #将current_file作为函数print_all的参数print("And let's rewind,kind of like a tape")
rewind(current_file)print("Let's print three lines:")
print_a_line(current_line,current_file)  #首先输出当前行,然后是文件第一行的内容current_line = current_line + 1          #行数+1
print_a_line(current_line,current_file)  #因为readline是每行循环读取的,这时的输出文件第二行的内容current_line = current_line + 1
print_a_line(current_line,current_file)

运行结果

PS E:\tonyc\Documents\Vs workspace\The Hard Way> python ‘.\ex20(函数和文件).py’ .\ex20_sample.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.
And 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.

如果去掉seek()的定位过程,也即注释掉rewind(current_file),运行结果如下,可以看到这时只有行数的显示,行内容并没有输出,因为之前执行了print_all函数,里面的read()默认是指针从文件头部到尾部,执行之后指针指向文件尾部,此时内容为空,所以要使用seek(0)重新定位至头部

PS E:\tonyc\Documents\Vs workspace\The Hard Way> python ‘.\ex20(函数和文件).py’ .\ex20_sample.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.
And let’s rewind,kind of like a tape
Let’s print three lines:
1
2
3

总结

  • 可以使用current_line += 1来简写

ex20 函数和文件相关推荐

  1. 《Learn python3 the hard way》ex20函数和文件

    1-seek(0)用于重置指针到文件开头的位置,重新开始 因为在打印完全文时光标已至文尾,要重新打印一行一行需要光标回到开头. 2-readline()是按行来读文件,所以每次打印时只打印一行. fr ...

  2. 笨方法学python: ex20, 函数和文件

    练习目的: 在函数中包括对文件的操作 转载自: http://www.cnblogs.com/rollenholt/archive/2012/04/23/2466179.html python中对文件 ...

  3. 【“笨办法”学Python】20.函数和文件

    20.函数和文件 文章目录 前言 一.Atom文本编辑器 二.运行Python程序 总结 前言   函数和文件在一起是如何一起协助发挥作用. 一.Atom文本编辑器 from sys import a ...

  4. python基础---元组、字典、函数、文件、异常

    文章目录 python基础---元组.字典.函数.文件.异常 Tuple(元组) 常用操作 dict(字典) 函数 文件 异常 python基础-元组.字典.函数.文件.异常 Tuple(元组) tu ...

  5. 使用Windows的SHFileOperation外壳函数实现文件操作

    在Windows的shellapi文件中定义了一个名为SHFileOperation()的外壳函数,用它可以实现各种文件操作,如文件的拷贝.删除.移动等,该函数使用起来非常简单,它只有一个指向SHFI ...

  6. Matlab编程与数据类型 -- 函数M文件的组成

    本图文介绍了Matlab中函数M文件的组成.

  7. Matlab编程与数据类型 -- 函数M文件的调用

    本图文介绍了Matlab中函数M文件的调用方式.

  8. pandas将列表list插入到dataframe的单元格中、pandas使用read_csv函数读取文件并设置保留数值的前置0( leading zeroes)

    pandas将列表list插入到dataframe的单元格中.pandas使用read_csv函数读取文件并设置保留数值的前置0( leading zeroes) 目录

  9. pandas使用read_csv函数读取文件并解析日期数据列(parse dates)、pandas使用read_csv函数读取文件并将缺失值转化为空字符串

    pandas使用read_csv函数读取文件并解析日期数据列(parse dates).pandas使用read_csv函数读取文件并将缺失值转化为空字符串 目录

最新文章

  1. 2018-4-8使用兔子的例子对比说明遗传算法,局部搜索,禁忌搜索,模拟退火方法
  2. 如何在现有的ul中添加li?
  3. 二十一.HTTP属性管理
  4. python分析推特_用Python关注者的关注者抓取Twitter数据的最快方法
  5. 剑指offer 26.数字的子结构
  6. java 算法 福尔摩斯的约会
  7. MSCI发布最新ESG评级 三七互娱位居A股游戏行业最高
  8. [Lintcode]66. Binary Tree Preorder Traversal/[Leetcode]144. Binary Tree Preorder Traversal
  9. Android Studio实现一个校园二手交易系统
  10. 软件测试证书(1)— 软件评测师
  11. 【Android安全】fastboot相关
  12. 易用宝项目记录day1-springdatajpa
  13. VR全景智慧城市虚拟现实三维的发展
  14. python源码文件的后缀名_Python 源代码程序编译后的文件扩展名为_________。_学小易找答案...
  15. Unity与iOS相互调用
  16. 最便宜的方式学习阿里云产品之使用竞价式实例ECS
  17. 【Python】字符串转换为ASCII码
  18. linux桌面分辨率太高,将Ubuntu调整到高DPI分辨率屏幕
  19. 刷完这50个标准库模块:没人比我更懂Python了
  20. 复制粘贴-实现动态爱心 网页版

热门文章

  1. (第五天)编写训练记忆软件--扑克编码V2.0
  2. easyui easyui-textbox自定义验证
  3. 猫架的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  4. Echarts 地图tooltip多行显示,当鼠标悬停地区显示数据
  5. 目标检测训练数据旋转python代码——数据增广(一)
  6. nlp subword(BPE、ULM)简介
  7. PyTorch Image Models(timm)库基础
  8. IOTE 2019第十一届国际物联网展--苏州站
  9. C语言课程设计-Mp3播放器(1)
  10. 来自亚马逊CEO Jeff Bezos的20句经验之谈