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.

Answer from HolderRoberts on Stack Overflow
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'
Discussions

python - Reading contents of buffer line by line - Stack Overflow
I have to parse contents of printfbuf line by line, how can I do it? I tried the following code but it prints one character per one line: import StringIO import ctypes printbuffer = ctypes. More on stackoverflow.com
🌐 stackoverflow.com
March 3, 2017
input - Python read from buffer - Stack Overflow
i want a getch/cin like function in python that wont interrupt the program from running (wanna make flappyBird) return pressed key ascii code if anything pressed or returns nothing or a default val... More on stackoverflow.com
🌐 stackoverflow.com
June 10, 2018
Buffer/stream to read a subset of a file in Python? - Stack Overflow
I have written a class which parses the header segment of data file (for storing scientific instrument data) and collects things like offsets to various data segments within the file. The actual da... More on stackoverflow.com
🌐 stackoverflow.com
Get buffer size
You could try using the read1() method of the BufferedReader object . Read a number of bytes and while you get the number of bytes you asked for keep reading until you get a "short" read. I haven't done this myself, but maybe that will work for you. More on reddit.com
🌐 r/learnpython
6
3
October 22, 2023
🌐
Python.org
discuss.python.org › python help
Reading data from a binary buffer - Python Help - Discussions on Python.org
May 24, 2024 - I have a bytes buffer called data and need to read its content. It was created by a C++ program and sent over the network. Its layout is known, but a has a variable length. Here is an example: print(data) id = int.from_bytes(data[:4], byteorder='little') w = int.from_bytes(data[4:8], byteorder='little') h = int.from_bytes(data[8:12], byteorder='little') a = np.frombuffer(data[12:], dtype='f4') print(id, w, h, a) It produces: b'\x13\x00\x00\x00{\x00\x00\x00\xc8\x01\x00\x00ff\x86@ff\x06@\xcd\xc...
🌐
Program Creek
programcreek.com › python
Python read buffer
def read_buffer(buffer, pointer): """ Read an element from 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-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?
May 28, 2025 - Use read() without parameters to read entire small files as strings. For large files, use buffered reading with read(buffer_size) to manage memory efficiently.
Find elsewhere
🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › functions › buffer.html
buffer — Python Reference (The Right Way) 0.1 documentation
>>> b = buffer('hello') >>> b <read-only buffer for 0x00EE38A0, size -1, offset 0 at 0x00EE3020> >>> print b hello >>> b = buffer('hello', 1) >>> print b ello >>> b = buffer('hello', 1, 3) >>> print b ell
🌐
Python Morsels
pythonmorsels.com › how-does-file-buffering-work
How does file buffering work? - Python Morsels
October 28, 2022 - But Python isn't going to disk to read another line for each iteration of our loop, because that would be inefficient. Instead, Python stores a buffer of bytes in the BufferedReader object that our text file wraps around, and then our text file keeps track of where it left off within that buffer. If we consume just one line from our file and then we ask our file for its current position using the file tell method, it'll tell us that it's not very far along:
🌐
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 ...
🌐
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
🌐
Medium
medium.com › @AlexanderObregon › how-pythons-memoryview-and-buffers-work-23c489fbbabb
How Python’s Memoryview and Buffers Work | Medium
August 11, 2024 - This capability is essential for efficient data manipulation, especially when dealing with large datasets. The memoryview object exposes the buffer protocol of an object, allowing you to read and write to the memory directly.
🌐
GitHub
github.com › simonw › til › blob › main › python › io-bufferedreader.md
til/python/io-bufferedreader.md at main · simonw/til
import io import sys import csv # Get a file-like object in binary mode fp = open("myfile.csv", "rb") # Or from standard input (need to use .buffer here) fp = sys.stdin.buffer # Wrap it in a buffered reader with a 4096 byte buffer buffered = io.BufferedReader(fp, buffer_size=4096) # Wrap THAT in a text io wrapper that can decode to unicode decoded = io.TextIOWrapper(buffered, encoding="utf-8") # Now I can read the first 2048 bytes...
Author: simonw
🌐
Eli Bendersky
eli.thegreenplace.net › 2011 › 11 › 28 › less-copies-in-python-with-the-buffer-protocol-and-memoryviews
Less copies in Python with the buffer protocol and memoryviews - Eli Bendersky's website
November 28, 2011 - Use the PyArg_ParseTuple function with the w* format specifier to parse its argument as a R/W buffer object, which itself calls PyObject_GetBuffer - a Python API that invokes the producer's "get buffer" function. Read data from the file directly into this buffer.
🌐
Stack Overflow
stackoverflow.com › questions › 50787003 › python-read-from-buffer
input - Python read from buffer - Stack Overflow
June 10, 2018 - i want a getch/cin like function in python that wont interrupt the program from running (wanna make flappyBird) return pressed key ascii code if anything pressed or returns nothing or a default value if no key pressed
🌐
Python
docs.python.org › 3 › library › io.html
io — Core tools for working with streams
Base class for binary streams that support some kind of buffering. It inherits from IOBase. The main difference with RawIOBase is that methods read(), readinto() and write() will try (respectively) to read as much input as requested or to emit all provided data.
🌐
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)
🌐
Reddit
reddit.com › r/learnpython › get buffer size
r/learnpython on Reddit: Get buffer size
October 22, 2023 -

I have a BytesIO buffer (BufferedReader) I need a way to read it's current data, I can't just use read() because the read function is waiting for an EOF char, but because the buffer is not complete yet it doesnt have that char.

I need a way to read all the data that the buffer has as this moment. Maybe get the length of the data in there, etc.