Python对文件的基础操作间python基础教程总结:Python 文件I/O部分;以下主要总结大文件和小文件操作过程中内存有效利用方法。
小文件
with函数(推荐使用)
The with
statement handles opening and closing the file, including if an exception is raised in the inner block.1
2
3with open('myfile') as f:
for line in f:
<do something with line>
readlines/readline
1 | for line in open('myfile','r').readlines(): |
二者的区别是readlines
读进来的是列表,而readline
是字符串;1
2
3
4
5
6
7
8
9
10import re
with open('zsq.txt') as f:
lines = f.readlines()
print type(lines)
<type 'list'>
import re
with open('zsq.txt') as f:
lines = f.readline()
print type(lines)
<type 'str'>
大文件
fileinput
1 | import fileinput |
fileinput.input()
call reads lines sequentially, but doesn’t keep them in memory after they’ve been read or even simply so this.
with
处理多个文件:1 | with fileinput.input(files=('spam.txt', 'eggs.txt')) as f: |
buffer
1 | filePath = "input.txt" |
贡献来源
http://stackoverflow.com/questions/8009882/how-to-read-large-file-line-by-line-in-python?noredirect=1&lq=1