• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python3读取文件常用方法实例分析

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

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python3实例,python3爬虫实例,python3实例教程,python3程序实例,python3项目实例等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了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)

</div>

输出如下:

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'

</div>

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

</div>

您可能想查找下面的文章:

  • 详解python3百度指数抓取实例
  • python3序列化与反序列化用法实例
  • Python3写入文件常用方法实例分析
  • Python3读取文件常用方法实例分析
  • Python3基础之输入和输出实例分析
  • Python3基础之条件与循环控制实例解析
  • Python3基础之list列表实例解析
  • Python3基础之输入和输出实例分析
  • Python3基础之条件与循环控制实例解析
  • Python3基础之list列表实例解析

相关文章

  • 通过Python来使用七牛云存储的方法详解
  • Python映射拆分操作符用法实例
  • 在 Python 应用中使用 MongoDB的方法
  • 利用Python自动监控网站并发送邮件告警的方法
  • python计数排序和基数排序算法实例
  • Python编程语言的35个与众不同之处(语言特征和使用技巧)
  • python脚本设置超时机制系统时间的方法
  • python脚本监控docker容器
  • 讲解Python中for循环下的索引变量的作用域
  • 跟老齐学Python之啰嗦的除法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python multiprocessing.Manager介绍和实例(进程间共享数据)
    • Python中的并发编程实例
    • Python遍历目录中的所有文件的方法
    • python清除字符串里非字母字符的方法
    • Python urlopen()函数 示例分享
    • 使用Python脚本将文字转换为图片的实例分享
    • python爬取51job中hr的邮箱
    • pyside写ui界面入门示例
    • Python实现二分查找与bisect模块详解
    • 使用Python读写及压缩和解压缩文件的示例

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有