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'
🌐
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.
Discussions

Reading data from a binary buffer
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_… More on discuss.python.org
🌐 discuss.python.org
7
0
May 24, 2024
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
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 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.
🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › functions › buffer.html
buffer — Python Reference (The Right Way) 0.1 documentation
See memoryview() for read and write access. >>> 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.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...
🌐
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 - 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)}")
Find elsewhere
🌐
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 - On the consumer side, the code that interests us is the buffered_readinto function in Modules\_io\bufferedio.c [6]. I won't show its full code here since it's quite complex, but with regards to the buffer protocol, the flow is simple: 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.
🌐
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 ...
🌐
Medium
medium.com › @AlexanderObregon › how-pythons-memoryview-and-buffers-work-23c489fbbabb
How Python’s Memoryview and Buffers Work | Medium
August 11, 2024 - By the end of this article, you’ll have a better understanding of how to leverage these tools for efficient data handling in your Python applications. Memoryview objects provide a way to access the internal data of an object that supports the buffer protocol without copying it. 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.
🌐
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
🌐
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)
🌐
TutorialsPoint
tutorialspoint.com › 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?
May 15, 2025 - 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.
🌐
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.

🌐
Stack Overflow
stackoverflow.com › questions › 13851897 › python-file-read-buffer-boundary
python File read buffer boundary - Stack Overflow
May 23, 2017 - The part corresponding to the last 10 lines in the buffer won't be read but it doesn't matter. ... If you use the read method without any arguments it will return the entire file content. You can use the size parameter if you only want to read a part of the file at a time. See the documentation for more info http://docs.python.org/2.7/tutorial/inputoutput.html#reading-and-writing-files
🌐
DEV Community
dev.to › redhap › a-neat-python-buffer-read-iterable-279k
A neat Python buffer read iterable. - DEV Community
September 2, 2022 - with open('eek.txt', 'rt') as f: # Use whatever chunk size you want for buff in iter(lambda: f.read(1024), ''): # Super-cool stuff using a 1K buffer!!
🌐
Python
docs.python.org › 2.0 › api › bufferObjects.html
7.2.3 Buffer Objects
Parameters and exceptions are similar to those for PyBuffer_FromObject(). If the base object does not export the writeable buffer protocol, then TypeError is raised. PyObject* PyBuffer_FromMemory (void *ptr, int size) Return value: New reference. Return a new read-only buffer object that reads from a specified location in memory, with a specified size.
🌐
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