We sometimes have files with binary data, i.e., where you convert int
to binary then encode it into the file — this is generally a pretty bad idea, because int
and float
numbers are encoded differently on different systems.
The exceptions are .jpeg and .mpeg. To IO binary, you need to use write()
.
Magic numbers
One key aspect of file metadata is the magic number, which is used to identify the file format of a given file. These consist of several carefully encoded bytes at the beginning of the file, distinct for the file format.