如何读取以大文件,可以使用read(size),一次最多读取size个字节。
100G也是可以读取的
def read_in_chunks(file_object, chunk_size=1024): """把文件切割成一块块的读,默认大小是1024k""" while True: data = file_object.read(chunk_size) if not data: break yield data
1
2
3
4
5
6
7
8
|
def
read_in_chunks
(
file_object
,
chunk_size
=
1024
)
:
"""把文件切割成一块块的读,默认大小是1024k"""
while
True
:
data
=
file_object
.
read
(
chunk_size
)
if
not
data
:
break
yield
data
|
def read_in_chunks(file_object, chunk_size=1024): """把文件切割成一块块的读,默认大小是1024k""" while True: data = file_object.read(chunk_size) if not data: break yield data f = open('new.log') for piece in read_in_chunks(f): process_data(piece)
1
2
3
4
5
6
7
8
9
10
11
12
13
|
def
read_in_chunks
(
file_object
,
chunk_size
=
1024
)
:
"""把文件切割成一块块的读,默认大小是1024k"""
while
True
:
data
=
file_object
.
read
(
chunk_size
)
if
not
data
:
break
yield
data
f
=
open
(
'new.log'
)
for
piece
in
read_in_chunks
(
f
)
:
process_data
(
piece
)
|