Python按行读取文件的实现方法【小文件和大文件读取】
本文实例讲述了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文本文件操作技巧汇总》、《Python URL操作技巧总结》、《Python图片操作技巧总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。
相关推荐
-
Python按行读取文件的简单实现方法
1:readline() file = open("sample.txt") while 1: line = file.readline() if not line: break pass # do something file.close() 一行一行得从文件读数据,显然比较慢: 不过很省内存: 测试读10M的sample.txt文件,每秒大约读32000行: 2:fileinput import fileinput for line in fileinput.input("
-
Python文件读取的3种方法及路径转义
1.文件的读取和显示 方法1: 复制代码 代码如下: f=open(r'G:\2.txt') print f.read() f.close() 方法2: 复制代码 代码如下: try: t=open(r'G:\2.txt') print t.read() finally: if t: t.close() 方法3: 复制代码 代码如下: with open(r'g:\2.txt') as g: for line in g:
-
python简单读取大文件的方法
本文实例讲述了python简单读取大文件的方法.分享给大家供大家参考,具体如下: Python读取大文件(GB级别)采用的办法很简单: with open(...) as f: for line in f: <do something with line> 例如: with open(filepath,'r') as infile: for line in infile: print line 一切都交给python解释器处理,读取效率很高,且占用资源少. stackoverflow参考链接:
-
python逐行读取文件内容的三种方法
方法一: 复制代码 代码如下: f = open("foo.txt") # 返回一个文件对象 line = f.readline() # 调用文件的 readline()方法 while line: print line, # 后面跟 ',' 将忽略换行符 # print(line, end = '') # 在 Python 3中使用 line = f.readline()
-
Python3读取zip文件信息的方法
本文实例讲述了Python3读取zip文件信息的方法.分享给大家供大家参考.具体实现方法如下: 该程序接受一个字符串,其内容是一个zip文件,需要读取这个zip文件中的信息 import zipfile class zip_string(zipfile.ZipFile): def __init__(self, data_string): zipfile.ZipFile.__init__(self, data_string) zstr = zip_string('d:/中华十大名帖.zip') f
-
python使用fileinput模块实现逐行读取文件的方法
本文实例讲述了python使用fileinput模块实现逐行读取文件的方法.分享给大家供大家参考.具体实现方法如下: #-------------------------------- # Name: read_lines.py # Author: Kevin Harris # Last Modified: 02/13/04 # Description: This Python script demonstrates # how to use fileinput to read # each l
-
Python多进程分块读取超大文件的方法
本文实例讲述了Python多进程分块读取超大文件的方法.分享给大家供大家参考,具体如下: 读取超大的文本文件,使用多进程分块读取,将每一块单独输出成文件 # -*- coding: GBK -*- import urlparse import datetime import os from multiprocessing import Process,Queue,Array,RLock """ 多进程分块读取文件 """ WORKERS = 4
-
Python读取一个目录下所有目录和文件的方法
本文实例讲述了Python读取一个目录下所有目录和文件的方法.分享给大家供大家参考,具体如下: 这里介绍的是刚学python时的一个读取目录的列子,给大家分享下: #!/usr/bin/python # -*- coding:utf8 -*- import os allFileNum = 0 def printPath(level, path): global allFileNum ''' 打印一个目录下的所有文件夹和文件 ''' # 所有文件夹,第一个字段是次目录的级别 dirList = [
-
Python linecache.getline()读取文件中特定一行的脚本
比如: Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->import linecacheprint linecache.getline('2.1_open.py', 4)将返回我上一节事例代码文件2.1_open.py的第4行文字,输出结果:f = open('/home/evergreen/桌面/test') 查看linecache中的实现(我
-
Python3读取文件常用方法实例分析
本文实例讲述了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
随机推荐
- Extjs中ComboBoxTree实现的下拉框树效果(自写)
- 基于AngularJS的拖拽文件上传的实例代码
- 上海居住证办理 续办上海居住证全攻略
- [PHP]常用正则表达式收集
- MyBatis持久层框架的用法知识小结
- php将字符串全部转换成大写或者小写的方法
- php实现复制移动文件的方法
- C语言判断字符是否为可打印字符的方法
- JavaScript 节点操作 以及DOMDocument属性和方法
- mysql存储过程中的异常处理解析
- 浅谈PHP中foreach/in_array的使用
- PHP垃圾回收机制简单说明
- jquery遍历函数siblings()用法实例
- ASP 包含文件中的路径问题和使用单一数据库连接文件的解决方案
- 深入理解JavaScript是如何实现继承的
- jQuery中queue()方法用法实例
- jquery dialog open后,服务器端控件失效的快速解决方法
- 谨慎使用Java8的默认方法
- 随时给自己贴的图片加文字的php代码
- Python中数组,列表:冒号的灵活用法介绍(np数组,列表倒序)