手机
当前位置:查字典教程网 >脚本专栏 >python >Python按行读取文件的实现方法【小文件和大文件读取】
Python按行读取文件的实现方法【小文件和大文件读取】
摘要:本文实例讲述了Python按行读取文件的实现方法。分享给大家供大家参考,具体如下:小文件:#coding=utf-8#author:walk...

本文实例讲述了Python按行读取文件的实现方法。分享给大家供大家参考,具体如下:

小文件:

#coding=utf-8 #author: walker #date: 2013-12-30 #function: 按行读取小文件 all_lines = [] try: file = open('txt.txt', 'r') all_lines = file.readlines() except IOError as err: print('File error: ' + str(err)) finally: if 'file' in locals(): file.close() for line in all_lines: print(line)

大文件:

#coding=utf-8 #author: walker #date: 2013-12-30 #function: 按行读取大文件 try: file = open('txt.txt', 'r') for line in file: print(line) except IOError as err: print('File error: ' + str(err)) finally: if 'file' in locals(): file.close()

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

【Python按行读取文件的实现方法【小文件和大文件读取】】相关文章:

python实现DNS正向查询、反向查询的例子

python 文件与目录操作

Python下singleton模式的实现方法

python 测试实现方法

python中的sort方法使用详解

python冒泡排序算法的实现代码

Python读写Excel文件的实例

python读取注册表中值的方法

python 实现文件的递归拷贝实现代码

python用ConfigObj读写配置文件的实现代码

精品推荐
分类导航