Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read binary file
#1
Hi, I am beginner and I need to read binary files.
How can I read each forth(nth) chunk of 1024 bytes.
Reply
#2
What have you tried? Please post code with issue.
Reply
#3
open with mode 'rb', example:
chunksize = 256 # Or whatever size you want to read in at a time
with open('MyFilename', 'rb') as f:
    for chunk in iter(lambda: f.read(chunksize), b""):
        ....
Reply
#4
(Feb-15-2018, 10:13 AM)ammann Wrote: Hi, I am beginner and I need to read binary files.
How can I read each forth(nth) chunk of 1024 bytes.

Larz60+ solution is to read the whole file in chunks.
Taking this example and extending this with enumerate.

chunksize = 256 # Or whatever size you want to read in at a time
with open('MyFilename', 'rb') as f:
    for n, chunk in enumerate(iter(lambda: f.read(chunksize), b"")):
        if n % 4 != 0:
            continue
simplified


from functools import partial
# to get rid of the lambda


chunksize = 1024
with open('MyFilename', 'rb') as f:
    reader_partial = partial(f.read, chunksize)
    reader_iterator = iter(reader_partial, b"")
    # iter iterates until it get a an empty byte string
    for n, chunk in enumerate(reader_iterator):
        if n % 4 != 0:
            continue
        # do something every 4th 1024 bytes
        # code...
This example and the previous example do read the full file.
Also parts are read, which should not be processed.
You can change this, if you use the methods tell and seek on the file-object.

With this approach, you should get every 4th 1024 bytes started at zero:

with open('/bin/sh', 'rb') as fd:
    while True:
        print('At position:', fd.tell())
        chunk = fd.read(1024)
        print('Reading 1024 bytes')
        if not chunk:
            break
        fd.seek(3 * 1024, 1)


I'm not sure if I made a mistake with the factor 3 in fd.seek.
I use fd.seek(n, 1) to move the pointer in the file to the relative position n.
fd.seek(n, 0) is a absolute move to the position n.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
Thanks for answers, but I have a problem again.
chunksize = 1024
new_image = []


with open('damaged.jpg', 'rb') as f:
    print(f.read(1024))
    for n, chunk in enumerate(iter(lambda: f.read(chunksize), b"")) :
        if (n-1)%3 != 0 :
            new_image.append(chunk)
        else:
            new_image.append(chunk[::-1])


print(len(new_image))

with open('new.jpg', 'wb') as new:
    i = 0
    while i+1 <= len(new_image):
        new.write(new_image[i])
        i += 1
I wrote all chunks to new_image, but size of damaged.jpg is 57984 and size of new.jpg is 56960. So how I lost 1 chunk?
Edited: Oh, i find my mistake in 6th line.
Reply
#6
Copy a file?

with open('image.jpg', 'rb') as in_file:
    with open('new_image.jpg', 'wb') as out_file:
        chunk = 4096

        while True:
            chnk = in_file.read(chunk)
            if chnk:
                out_file.write(chnk)
            else:
                break
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Recommended way to read/create PDF file? Winfried 3 2,785 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,314 Nov-09-2023, 10:56 AM
Last Post: mg24
  read file txt on my pc to telegram bot api Tupa 0 1,052 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,043 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,162 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 6,016 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Read csv file with inconsistent delimiter gracenz 2 1,149 Mar-27-2023, 08:59 PM
Last Post: deanhystad
  Read text file, modify it then write back Pavel_47 5 1,501 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  Correctly read a malformed CSV file data klllmmm 2 1,814 Jan-25-2023, 04:12 PM
Last Post: klllmmm
  How to read csv file update matplotlib column chart regularly SamLiu 2 1,016 Jan-21-2023, 11:33 PM
Last Post: SamLiu

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020