Reputation: 49
I am working on reading specific holter data (.ecg files) and copying specific data, as specified by the user, into a new file. Basically, I only want to read the data into memory that is necessary. Is there a way to use .seek() to only read in data bytes from (start, end)?
start = args.packet_start
end = args.packet_end
try:
print("Beginning copying of holter data...")
# Output the specific holter data
output_file = open("copied_holter.ecg", 'w')
# Read part of holter file into memory
holter = open(args.filename, 'rb')
data = holter.seek()
# Parse through holter data and copy to file
for index in range(start, end+1):
data_list = data[index]
output_file.write(data_list)
# Close the file streams
holter.close()
output_file.close()
except Exception as e:
print(e)
print("Exiting program, due to exception.")
exit(1)
print "Finished data copying operations!"
Upvotes: 2
Views: 926
Reputation: 5184
seek
will move the pointer to specified location, not return data. read
willy return number of bytes specified. Also you should use with
statement when working with files or file like objects.
with open(args.filename, 'rb') as holter, open("copied_holter.ecg", 'w') as output_file:
holter.seek(start)
data = holter.read(end-start)
output_file.write(data)
Upvotes: 4