'rb' mode allows you to read the source binary data from a file in Python:
with open(filename, 'rb') as file: raw_binary_data = file.read()
type(raw_binary_data) == bytes . bytes is an immutable byte sequence in Python.
Do not confuse bytes with their textual representation: print(raw_binary_data) will show you a textual representation of the data, for example, byte 127 (base 10: decimal), which you can represent as bin(127) == '0b1111111' (base 2: binary) or as hex(127) == '0x7f' (base 16: hexadecimal) is displayed as b'\x7f' (seven ascii characters are output). Bytes from the printable ascii range are represented as the corresponding ascii characters, for example, b'\x41' displayed as b'A' ( 65 == 0x41 == 0b1000001 ).
Byte
0x7f not saved on disk as seven ascii 1111111 binary digits, it is not saved as two ascii hexadecimal digits: 7F , it is not saved as three 127 decimal digits. b'\x7f' is a textual representation of a byte that can be used to indicate it in the Python source code (you won’t find the literally seven characters ascii b'\x7f' on disk either) This code writes one byte to disk:
with open('output.bin', 'wb') as file: file.write(b'\x7f')
Some characters should be used to represent bytes, what are they?
OS interfaces (a way to access hardware such as disks) are defined in bytes, for example POSIX read (2) , i.e. bytes are the fundamental unit here: you can directly read / write bytes - you don't need any intermediate representation. See Richard Feynman. Why.
How bytes appear physically between OS drivers and hardware - it can be anything - you do not need to worry about it: it is hidden behind a single OS interface. See How are data physically written, read, and stored on hard drives?
You can call os.read() directly in Python, but you don't need it; file.read() does this for you (Python 3 objects are directly implemented on top of the POSIX interface. I / O Python 2 uses the C stdio library, which in turn uses the OS interfaces to implement its functions).
As you pointed out, it is the OS drivers and hardware that determine how bytes are written, but the Python interpreter could read them. So he is reading something - what is it? This is not a reading of the magnetic orientation of the particles on the disk, is it? He is reading something symbolic, and I want to access him.
He reads bytes. A hard drive is a small computer, and therefore it may be interesting, but it will not change that it is completely omitted (as far as "symbolic" or software).
The book "CODE" The Hidden Language of Computer Hardware and Software " gives a very gentle idea of how information is presented on computers - the word" byte "is not defined until page 180. To view the levels of abstraction used on computers, the course" From NAND up to Tetris' can help .