Answer the question
In order to leave comments, you need to log in
Byte-by-byte reading from a certain segment of the file?
Good afternoon!
There is a file inside the traffic (packet header + packet), I can read the file from the beginning and decode it in the right way. The problem is that sometimes I do not need to read the entire file, but subtract a range of bytes, I have to read the file from the beginning to the cut point.
For example, there is a file with a size of 12345 bytes, I need to cut a segment from 1234 to 7890 bytes from it.
Here is my working code, the question is whether it is possible to set a slice?
begin_f = 1234
end_f = 7890
n = 0
with open(file, 'rb') as ifile:
while True:
one_byte = ifile.read(1) # думал делать размер чанка == (begin_f - 1), но плохо для больших файлов.
if n == begin_f:
with open(output_file, 'wb') as ofile:
data = one_byte + ifile.read(end_f - begin_f - 1)
ofile.write(data)
break
n += 1
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question