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 相关文章推荐
Python urllib模块urlopen()与urlretrieve()详解
Nov 01 Python
举例讲解Python中is和id的用法
Apr 03 Python
Python3.2模拟实现webqq登录
Feb 15 Python
Python抓取框架 Scrapy的架构
Aug 12 Python
Python3实现的判断回文链表算法示例
Mar 08 Python
pandas进行时间数据的转换和计算时间差并提取年月日
Jul 06 Python
Python一行代码解决矩阵旋转的问题
Nov 30 Python
python使用正则来处理各种匹配问题
Dec 22 Python
python对接ihuyi实现短信验证码发送
May 10 Python
python3获取控制台输入的数据的具体实例
Aug 16 Python
python 中的9个实用技巧,助你提高开发效率
Aug 30 Python
Python爬虫实现selenium处理iframe作用域问题
Jan 27 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
Syphon 秘笈
2021/03/03 冲泡冲煮
基于mysql的论坛(5)
2006/10/09 PHP
PHP实现统计在线人数功能示例
2016/10/15 PHP
PHP的中使用非缓冲模式查询数据库的方法
2017/02/05 PHP
ThinkPHP中类的构造函数_construct()与_initialize()的区别详解
2017/03/13 PHP
php下载远程大文件(获取远程文件大小)的实例
2017/06/17 PHP
基于win2003虚拟机中apache服务器的访问
2017/08/01 PHP
使用jscript实现二进制读写脚本代码
2008/06/09 Javascript
jquery 上下滚动广告
2009/06/17 Javascript
JavaScript 三种不同位置代码的写法
2009/10/25 Javascript
基于Jquery+Ajax+Json的高效分页实现代码
2011/10/29 Javascript
json的前台操作和后台操作实现代码
2012/01/20 Javascript
jQuery.clean使用方法及思路分析
2013/01/07 Javascript
在JavaScript中typeof的用途介绍
2013/04/11 Javascript
jquery利用ajax调用后台方法实例
2013/08/23 Javascript
ECMAScript6函数剩余参数(Rest Parameters)
2015/06/12 Javascript
jQuery插件windowScroll实现单屏滚动特效
2015/07/14 Javascript
jQuery实现简单的网页换肤效果示例
2016/09/18 Javascript
jQuery插件FusionCharts实现的Marimekko图效果示例【附demo源码】
2017/03/24 jQuery
JavaScript常见JSON操作实例分析
2018/08/08 Javascript
Vuex的初探与实战小结
2018/11/26 Javascript
小程序Scroll-view上拉滚动刷新数据
2020/06/21 Javascript
一文总结学习Python的14张思维导图
2017/10/17 Python
Python实现批量读取图片并存入mongodb数据库的方法示例
2018/04/02 Python
python分治法求二维数组局部峰值方法
2018/04/03 Python
Python实现字典(dict)的迭代操作示例
2018/06/05 Python
python实现京东秒杀功能
2018/07/30 Python
python实现文字版扫雷
2020/04/24 Python
python编写扎金花小程序的实例代码
2021/02/23 Python
Born鞋子官网:Born Shoes
2017/04/06 全球购物
2014年母亲节寄语
2014/05/07 职场文书
活动主持人开场白
2015/05/28 职场文书
2016年优秀少先队辅导员事迹材料
2016/02/26 职场文书
python自动计算图像数据集的RGB均值
2021/06/18 Python
关于PostgreSQL JSONB的匹配和交集问题
2021/09/14 PostgreSQL
vue判断按钮是否可以点击
2022/04/09 Vue.js