The Pythonic way is to write a generator function. Since this generator handles a file resource which needs to be closed afterwards, we should wrap it in a contextmanager, and consume the chunks within a with block.

from contextlib import contextmanager

@contextmanager
def file_chunks(filename, chunk_size):
    f = open(filename, 'rb')
    try:
        def gen():
            b = f.read(chunk_size)
            while b:
                yield b
                b = f.read(chunk_size)
        yield gen()
    finally:
        f.close()

Usage:

with file_chunks('test.raw', 160) as chunks:
    for chunk in chunks:
        # process the chunk
        print(chunk)
Answer from kaya3 on Stack Overflow
🌐
TutorialsPoint
tutorialspoint.com › article › how-an-entire-file-is-read-into-buffer-and-returned-as-a-string-in-python
How an entire file is read into buffer and returned as a string in Python?
March 24, 2026 - # Create a binary file with open('binary_sample.txt', 'wb') as file: file.write(b"Binary data: \x00\x01\x02\x03") # Read binary file in chunks buffer_size = 8 with open('binary_sample.txt', 'rb') as file: while True: chunk = file.read(buffer_size) if not chunk: break print(f"Binary chunk: {chunk}") Binary chunk: b'Binary d' Binary chunk: b'ata: \x00\x01\x02\x03' The io.StringIO module allows treating strings as file-like objects ? import io def read_string_into_buffer(data_string): buffer = io.StringIO(data_string) file_contents = buffer.read() buffer.close() return file_contents # Example usage data_string = "This is a string containing data that we want to read into a buffer." file_contents = read_string_into_buffer(data_string) print(file_contents) print(f"Type: {type(file_contents)}")
🌐
Python Lessons
pylessons.com › Python-3-basics-tutorial-writing-reading-file
Tutorial - Python 3 basics Writing and reading from files
October 18, 2018 - access_mode − The access_mode determines the mode in which a file has to be opened, i.e., read, write, append, etc. A complete list of possible values is given below in the list. This is an optional parameter and the default file access mode is read (r). buffering − If the buffering value is set to 0, no buffering takes place.
Top answer
1 of 3
2

You can read your data as a text file instead of binary file.

Here's how:

file = open("/dev/rtlightsensor0", "r")
line = file.readline()
data = [int(each) for each in line.split())

Original answer:

You didn't describe your question clearly enough, but I'm guessing that you have four 32-bit (4 bytes) int in your file, which you want to read into your program.

os.read is a binary file read function, so the variable data you got is bytes instead of a list of numbers.

Python can't infer the length your number as it can be four 32-bit numbers, one 128-bit number, or something else. You need to define it explicitly.

There is also the issue of whether your data are big endian or little endian.

If your data doesn't come from a network communication, it's likely to be little endian. If you find the result from the following code is wrong (for example, unbelievably large), you can try to convert 'big' into 'little'.

You can finish your code like this:

dev = os.open("/dev/rtlightsensor0", os.0_RDWR)
raw_data = os.read(dev,16)
data = []
data.append(int.from_bytes(raw_data[0:4], 'little'))
data.append(int.from_bytes(raw_data[4:8], 'little'))
data.append(int.from_bytes(raw_data[8:12], 'little'))
data.append(int.from_bytes(raw_data[12:16], 'little'))
print(data)

However, DO NOT do it like that because it's really silly. If you can understand the code above, then you can write it better like this:

dev = os.open("/dev/rtlightsensor0", os.0_RDWR)
raw_data = os.read(dev, 16)
data = [int.from_bytes(raw_data[i * 4: (i + 1) * 4], 'little') for i in range(4)]
print(data)

By the way, it's not wise to read binary data like this. Generally speaking, the package numpy is recommended.

2 of 3
1

It is a byte array. You can convert the byte array to string using decode function

>>> test = b'1 53 -5 1\n'
>>> type(test)
<class 'bytes'>
>>> test[0]
49
>>> test_1 = test.decode("utf-8")
>>> test_1[0]
'1'
🌐
MSU CSE
cse.msu.edu › ~ldillon › cse-ctl › Spring2018 › Meeting05 › filesOverview4up.pdf pdf
File Processing in Python Streams and Buffers
To open a text file in Python · my_file = open( "datafile.txt", "w" ) variable · creates and returns a file object containing a buffer, connected to · the named file, which is open for input or output · name of file · (on disk) access mode · (read or write) § ·
🌐
djangoSpin
djangospin.com › python-file-buffering
File Buffering in Python | Lakshay Arora | djangoSpin
March 29, 2017 - File Buffering in Python - Using the buffering argument in open() to make use of buffering while dealing with files. Lakshay Arora.
🌐
Stack Overflow
stackoverflow.com › questions › 44980766 › reading-a-file-into-a-string-buffer-in-python
Reading a file into a string buffer in python - Stack Overflow
July 8, 2017 - import ctypes, os from ctypes import * libc = CDLL('libc.so.6', use_errno = True) f=os.open('test.txt', os.O_RDONLY) if f < 0: print 'Cannot open file' buf = create_string_buffer(10) buf_ptr = ctypes.addressof(buf) print libc.read(f, buf_ptr, 5) e = get_errno() print str(e) + ': ' + os.strerror(e) print repr(buf.raw) ... What is going on, what am I missing? Just to be sure, I coded this in C as well and it works just fine. Any suggestions? EDIT: It might be worth mentioning that the code this came from reads directly from a drive into a memory aligned buffer.
🌐
Python Morsels
pythonmorsels.com › how-does-file-buffering-work
How does file buffering work? - Python Morsels
October 28, 2022 - Reading from a text file uses a buffer in Python and writing to files relies on a buffer as well.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 41171247 › buffer-stream-to-read-a-subset-of-a-file-in-python
Buffer/stream to read a subset of a file in Python? - Stack Overflow
However from there you will have to manually track the bytes you have read until you get to the max offset you are reading to. The start offset for seek() will of course have to be known beforehand as will the ending byte offset. Some example code follows (assuming a start offset of byte 250): import io stream = io.open("file.txt", "r") buffered_reader = io.BufferedReader(stream) # set the stream to byte 250 buffered_reader.seek(250) # read up to byte 750 (500 bytes from position 250) data = buffered_reader.read(500)
🌐
Apache Arrow
arrow.apache.org › docs › python › generated › pyarrow.BufferReader.html
pyarrow.BufferReader — Apache Arrow v25.0.1
Zero-copy reader from objects convertible to Arrow buffer. ... >>> import pyarrow as pa >>> data = b'reader data' >>> buf = memoryview(data) >>> with pa.input_stream(buf) as stream: ... stream.size() ... stream.read(6) ... stream.seek(7) ... stream.read(15) ... 11 b'reader' 7 b'data' ... Read this file completely to a local path or destination stream.
🌐
Windsooon
windsooon.github.io › 2019 › 08 › 14 › How Python read or write files under the hood
How Python read or write files under the hood · Tech blog of Unicooo
In Python 2, we use FILE streams (i.e. fopen, fclose, fread) for performance optimization. However, since The python community wants to be more reliably cross-platform than C FILE streams. Python 3 implements its user space cache (i.e. write buffer and read buffer).
🌐
Python
docs.python.org › 3 › c-api › buffer.html
Buffer Protocol — Python 3.14.8 documentation
An example consumer of the buffer interface is the write() method of file objects: any object that can export a series of bytes through the buffer interface can be written to a file. While write() only needs read-only access to the internal contents of the object passed to it, other methods such as readinto() need write access to the contents of their argument.
🌐
TutorialsPoint
tutorialspoint.com › article › How-do-we-specify-the-buffer-size-when-opening-a-file-in-Python
How do we specify the buffer size when opening a file in Python?
The buffering parameter in Python's open() function allows us to define how much data is stored in memory before being written to or read from the file.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › python bufferedreader
Python BufferedReader | Working of BufferedReader in Python | Examples
May 14, 2024 - The following article provides an outline for Python BufferedReader. It retrieves data from a source and stores it in an internal memory queue. This allows subsequent read operations to efficiently retrieve data from the buffered queue instead of directly accessing the underlying reader stream.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
TutorialsPoint
tutorialspoint.com › How-is-a-file-read-using-a-limited-buffer-size-using-Python
Python File read() Method
May 27, 2025 - The Python File read() method reads the contents of a file. This method reads the whole file, by default; and only specified bytes, if an optional argument is accepted. Even if the file contains more characters than the mentioned size, the remaining ...