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求两个list的差集、交集与并集的方法
Nov 01 Python
Python map和reduce函数用法示例
Feb 26 Python
Python中设置变量访问权限的方法
Apr 27 Python
Python搭建HTTP服务器和FTP服务器
Mar 09 Python
python交互式图形编程实例(一)
Nov 17 Python
Python使用装饰器模拟用户登陆验证功能示例
Aug 24 Python
通过字符串导入 Python 模块的方法详解
Oct 27 Python
python标识符命名规范原理解析
Jan 10 Python
Python配置pip国内镜像源的实现
Aug 20 Python
python 利用百度API识别图片文字(多线程版)
Dec 14 Python
python-jwt用户认证食用教学的实现方法
Jan 19 Python
Python使用永中文档转换服务
May 06 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 裁剪图片成固定大小代码方法
2009/09/09 PHP
解析PHP SPL标准库的用法(遍历目录,查找固定条件的文件)
2013/06/18 PHP
php 购物车完整实现代码
2014/06/05 PHP
PHP实现HTML生成PDF文件的方法
2014/11/07 PHP
php 将json格式数据转换成数组的方法
2018/08/21 PHP
JavaScript异步编程:异步数据收集的具体方法
2013/08/19 Javascript
jQuery中parentsUntil()方法用法实例
2015/01/07 Javascript
Jquery 实现弹出层插件
2015/01/28 Javascript
JavaScript插件化开发教程(六)
2015/02/01 Javascript
jQuery Real Person验证码插件防止表单自动提交
2015/11/06 Javascript
从零学习node.js之简易的网络爬虫(四)
2017/02/22 Javascript
深究AngularJS中ng-drag、ng-drop的用法
2017/06/12 Javascript
React复制到剪贴板的示例代码
2017/08/22 Javascript
JavaScript闭包的简单应用
2017/09/01 Javascript
vue点击按钮动态创建与删除组件功能
2019/12/29 Javascript
JS数组的常用10种方法详解
2020/05/08 Javascript
[00:35]DOTA2上海特级锦标赛 EG战队宣传片
2016/03/04 DOTA
Python 安装setuptools和pip工具操作方法(必看)
2017/05/22 Python
python中模块查找的原理与方法详解
2017/08/11 Python
Python语言描述连续子数组的最大和
2018/01/04 Python
利用Python+Java调用Shell脚本时的死锁陷阱详解
2018/01/24 Python
pycharm 在windows上编辑代码用linux执行配置的方法
2018/10/27 Python
pytorch中的自定义数据处理详解
2020/01/06 Python
python 绘制场景热力图的示例
2020/09/23 Python
python 解决函数返回return的问题
2020/12/05 Python
python Scrapy框架原理解析
2021/01/04 Python
CSS3 Flexbox中flex-shrink属性的用法示例介绍
2013/12/30 HTML / CSS
Myprotein蛋白粉美国官网:欧洲畅销运动营养品牌
2016/11/15 全球购物
台湾全方位线上课程与职能学习平台:TibaMe
2019/12/04 全球购物
什么是Linux虚拟文件系统VFS
2012/01/31 面试题
半年思想汇报
2013/12/30 职场文书
父母对孩子的寄语
2014/04/09 职场文书
高三英语复习计划
2015/01/19 职场文书
2015年保育员个人工作总结
2015/05/13 职场文书
RPM包方式安装Oracle21c的方法详解
2021/08/23 Oracle
Golang bufio详细讲解
2022/04/21 Golang