【发布时间】:2016-07-18 22:09:06
【问题描述】:
我正在尝试将字节 X 到 Y 的信息从一个巨大的数据文件复制并粘贴到一个新文件中。我通过使用 f.readline() 和 f.tell() 得到了 X 和 Y。有没有比下面的代码更快的方法。
import os
a = 300 # Beginning Byte Location
b = 208000 # Ending Byte Location
def file_split(x,y):
g = open('C:/small_file.dat', 'wb')
with open('C:/huge_data_file.dat', 'rb') as f:
f.seek(x, os.SEEK_SET) # Sets file pointer to x
line = '-1'
while (line != '') # line = '' would indicate EOF
while (f.tell() < y):
g.write(f.read(1))
g.close()
file_split(a,b)
【问题讨论】:
标签: python performance file copy-paste data-management