【问题标题】:Copy & Paste Information without f.read?在没有 f.read 的情况下复制和粘贴信息?
【发布时间】: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


    【解决方案1】:

    您可以从大于 1 字节的块大小开始吗?如果它永远不会是兆字节的数据,那就去 g.write(f.read(b-a)) 就完成了,不需要循环。如果要达到兆字节,您可能希望逐块进行,确保最后一个块更短,不超过 b。

    【讨论】:

      猜你喜欢
      • 2011-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-21
      • 1970-01-01
      • 2020-07-28
      • 2012-08-22
      相关资源
      最近更新 更多