# !/usr/bin/python3
import os, sys
# Open a file
fd = os.open("foo.txt",os.O_RDWR)
# Reading text
ret = os.read(fd,12)
print (ret.decode())
# Close opened file
os.close(fd)
print ("Closed the file successfully!!")
或者
with open(filename, 'rb') as f:
while True:
buf = f.read(max_size)
if not buf:
break
process(buf)
或者
from functools import partial
with open('somefile', 'rb') as openfileobject:
for chunk in iter(partial(openfileobject.read, 1024), b''):
do_something()
如果您运行的是64位操作系统,那么您可以^{} 导入整个文件,并让您的操作系统在后台为您进行读取。
mmap
ed文件大多与bytearray
呈现相同的接口,因此可以执行以下操作:然后您就可以使用
buf
作为一个普通的bytearray
,像这样的操作来迭代您的分隔符:但这只是一个演示。你可能想做一些更复杂的事情,也许在
yield
等之前从字节解码或者
或者
相关问题 更多 >
编程相关推荐