-1

I am trying to copy and paste information from bytes X to Y from a huge data file to a new file. I got X and Y by using f.readline() and f.tell(). Is there a faster way to do this then the code below.

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)

1 Answers1

0

You could start with a larger block size than 1 byte? If it's never going to be megabytes worth of data, just go for g.write(f.read(b-a)) and you're done, no need for the loop. If it's going to be megabytes, you may want to do it block by block, making sure the last block is shorter to not exceed b.

Wilmer
  • 113
  • 3