Python3读取文件常用方法实例分析


Posted in Python onMay 22, 2015

本文实例讲述了Python3读取文件常用方法。分享给大家供大家参考。具体如下:

''''' 
Created on Dec 17, 2012 
读取文件 
@author: liury_lab 
''' 
# 最方便的方法是一次性读取文件中的所有内容放到一个大字符串中: 
all_the_text = open('d:/text.txt').read() 
print(all_the_text) 
all_the_data = open('d:/data.txt', 'rb').read() 
print(all_the_data) 
# 更规范的方法 
file_object = open('d:/text.txt') 
try: 
  all_the_text = file_object.read() 
  print(all_the_text) 
finally: 
  file_object.close() 
# 下面的方法每行后面有‘\n'  
file_object = open('d:/text.txt') 
try: 
  all_the_text = file_object.readlines() 
  print(all_the_text) 
finally: 
  file_object.close() 
# 三句都可将末尾的'\n'去掉  
file_object = open('d:/text.txt') 
try: 
  #all_the_text = file_object.read().splitlines() 
  #all_the_text = file_object.read().split('\n') 
  all_the_text = [L.rstrip('\n') for L in file_object] 
  print(all_the_text) 
finally: 
  file_object.close() 
# 逐行读 
file_object = open('d:/text.txt') 
try: 
  for line in file_object: 
    print(line, end = '') 
finally: 
  file_object.close() 
# 每次读取文件的一部分 
def read_file_by_chunks(file_name, chunk_size = 100):   
  file_object = open(file_name, 'rb') 
  while True: 
    chunk = file_object.read(chunk_size) 
    if not chunk: 
      break 
    yield chunk 
  file_object.close() 
for chunk in read_file_by_chunks('d:/data.txt', 4): 
  print(chunk)

输出如下:

hello python
hello world
b'ABCDEFG\r\nHELLO\r\nhello'
hello python
hello world
['hello python\n', 'hello world']
['hello python', 'hello world']
hello python
hello worldb'ABCD'
b'EFG\r'
b'\nHEL'
b'LO\r\n'
b'hell'
b'o'

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
go语言计算两个时间的时间差方法
Mar 13 Python
用Python进行TCP网络编程的教程
Apr 29 Python
Python实现文件按照日期命名的方法
Jul 09 Python
Python脚本暴力破解栅栏密码
Oct 19 Python
详解Python with/as使用说明
Dec 13 Python
Python使用pandas对数据进行差分运算的方法
Dec 22 Python
解决python xlrd无法读取excel文件的问题
Dec 25 Python
python Pexpect 实现输密码 scp 拷贝的方法
Jan 03 Python
python如何变换环境
Jul 21 Python
零基础学python应该从哪里入手
Aug 11 Python
浅谈怎么给Python添加类型标注
Jun 08 Python
Python中的变量与常量
Nov 11 Python
在Python中处理时间之clock()方法的使用
May 22 #Python
Python3指定路径寻找符合匹配模式文件
May 22 #Python
Python3实现从指定路径查找文件的方法
May 22 #Python
在Python操作时间和日期之asctime()方法的使用
May 22 #Python
Python3遍历目录树实现方法
May 22 #Python
Python3实现将文件树中所有文件和子目录归档到tar压缩文件的方法
May 22 #Python
Python3读取zip文件信息的方法
May 22 #Python
You might like
php中使用addslashes函数报错问题的解决方法
2013/02/06 PHP
php去除二维数组的重复项方法
2015/11/03 PHP
php实现替换手机号中间数字为*号及隐藏IP最后几位的方法
2016/11/16 PHP
Ajax+PHP实现的模拟进度条功能示例
2019/02/11 PHP
php给数组赋值的实例方法
2019/09/26 PHP
PHP与Web页面的交互示例详解一
2020/08/04 PHP
JS使用eval()动态创建变量的方法
2016/06/03 Javascript
JS实现屏蔽网页右键复制及ctrl+c复制的方法【2种方法】
2016/09/04 Javascript
Vue.directive自定义指令的使用详解
2017/03/10 Javascript
原生JS实现的自动轮播图功能详解
2018/12/28 Javascript
jquery层次选择器的介绍
2019/01/18 jQuery
使用mixins实现elementUI表单全局验证的解决方法
2019/04/02 Javascript
微信小程序按钮点击跳转页面详解
2019/05/06 Javascript
js实现简单的随机点名器
2020/09/17 Javascript
python实现的一个火车票转让信息采集器
2014/07/09 Python
Python中的对象,方法,类,实例,函数用法分析
2015/01/15 Python
深入解析Python中的list列表及其切片和迭代操作
2016/03/13 Python
详解python中的 is 操作符
2017/12/26 Python
15行Python代码带你轻松理解令牌桶算法
2018/03/21 Python
查看python下OpenCV版本的方法
2018/08/03 Python
tensorflow实现加载mnist数据集
2018/09/08 Python
django用户登录验证的完整示例代码
2019/07/21 Python
详解python中自定义超时异常的几种方法
2019/07/29 Python
python 解决Windows平台上路径有空格的问题
2020/11/10 Python
详解WebSocket跨域问题解决
2018/08/06 HTML / CSS
吃透移动端 1px的具体用法
2019/12/16 HTML / CSS
韩国爱茉莉太平洋化妆品美国站:Amore Pacific US
2016/10/28 全球购物
do you have any Best Practice for testing
2016/06/04 面试题
汽车机修工岗位职责
2014/03/06 职场文书
《分一分》教学反思
2014/04/13 职场文书
2014年村官工作总结
2014/11/24 职场文书
员工辞职信范文
2015/03/02 职场文书
2015年业务工作总结范文
2015/04/10 职场文书
2015年卫生院健康教育工作总结
2015/07/24 职场文书
2019军训心得体会
2019/06/27 职场文书
nginx location优先级的深入讲解
2021/03/31 Servers