Lesson 8
Making Objects the Main Star!
Chapter 9: Reading and writing files
Files
You have so far opened and read some text files. Hopefully this is already natural to you by now!
Let’s spend a bit of time discussing files in a bit more detail.
What exactly are files?
A file is a named location on disk. It stores data on the hard disk.
The hard disk remembers your file even after you switch it off (it’s called non-volatile memory). Contrast this to a RAM which loses its data when you turn your computer off (volatile).
Binary vs text files
Python handles two types of files: binary and text files.
Text files are plain text files that a human can read. Your Python scripts are examples of text files. There are often \n
characters at the end of each line in the file to mark a new line (these may actually be \r\n
if the files were created on a Windows machine).
Most other files on your computer are binary files. For example image files like .jpg
and .png
, audio and video files like .mp3
or .avi
, and documents like .pdf
and .docx
. You cannot read the content of binary files directly in the text editor (try opening an image file in a text editor and you will only see garble!) They can only be viewed in an application that ‘understands’ how to interpret the file.