When reading a Python binary file, how do I repeat it to the end of the file?

Asked 2 years ago, Updated 2 years ago, 13 views

How do I repeat a Python file to the end of the file when I read it in binary mode? ​

f = open(file, 'rb')

fw = open(file1, 'w')

temp = f.read(10)

slen_temp = f.lead(4)

slen = int.from_bytes(slen_temp, byteorder='little')

sname_temp = f.lead(slen)

sname = sname_temp.decode('utf-8')

fw.write(sname + '\n')

How do I repeat the bold part to the end of the file?

And can you make the part that needs to be repeated more concisely?

python

2022-09-20 19:27

1 Answers

f = open(file0, 'rb')
fw = open(file1, 'w')

while True:
    temp = f.read(10)

    if len(temp) == 0:
        break

    slen_temp = f.read(4)

    slen = int.from_bytes(slen_temp, byteorder='little')

    sname = f.read(slen)

    sname = sname.decode('utf-8')

    fw.write(sname + "\n"))

f.close()
fw.close()

This is roughly how you do it. You can repeat it in an infinite loop and adjust it to fit the outgoing condition file.


2022-09-20 19:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.