Junior — Middle
What are the differences between files in text and binary formats?
sobes.tech AI
Answer from AI
Text files contain data in the form of readable characters, usually using encodings like UTF-8 or ASCII. They are intended for storing text and can be opened and edited with regular text editors.
Binary files contain data in the form of bytes, which can represent any information — images, audio, compiled code, etc. They are not meant for direct reading by humans and require special programs for interpretation.
Main differences:
- Readability: Text files are human-readable, binary files are not.
- Size: Binary files are often more compact, as they do not contain additional formatting characters.
- Processing: Text files can be processed line by line, binary files require reading in byte blocks.
Example in Python for opening files:
# Text file
with open('file.txt', 'r', encoding='utf-8') as f:
text = f.read()
# Binary file
with open('file.bin', 'rb') as f:
data = f.read()