Often programs needs to store information between sessions. There are several ways to do this. First is through the use of an external file.
Why not just keep the data in a variable? Well data that is store in RAM (Random Access Memory) is reused. And if an application ends, the data is erased and used by other applications. If the machine is (inadvertently) turned off, it will also be reset, and data lost. That is why we store the information externally.
The process of saving data is sometimes referred to as writing data. Where you are reading data when you take an input from a file.
Working with the File
Whether you read or write data, there are three basic steps that should occur when working with a file.
- Open the file – this creates a connection between your application and the file. There are different types of opening a file, for reading, writing, and appending are the most common. IF you open a file for reading, other people can usually open the file and read it at the same time. If you open to write or append, you will be editing the file, and most OSes won’t let someone else open the file as well at the same time as it doesn’t want you to overwrite what was put there.
- Process the file – this could be reading data, writing data, etc. Sometimes you will just read a little bit of the file, sometimes the whole file. It depends upon what you are doing, and the size of the file.
- Close the file – here you need to release the file, so others can continue to use it.
File Types
All files can be broken down into two basic types. Regardless of the type of file, the way you work with them is the same.
First is a text file. Text files are encoded and stored in ASCII or Unicode format. They can be opened by a text editor like Notepad on Windows. Binary file data hasn’t been encoded to work with one of those common text types. That means that you need to know how to read and write that data.
Now most applications work with binary data, either to secure their files so others cannot read them as easily, because they are not storing text data, or to provide some functionality that requires non-text data. This includes everything from image formats, zip files, and even word processors files. So just because you see text in the file, doesn’t mean that the file is stored as text.
Python can work with both of these types of files, however, we will work with text data as it is easier to look at the data outside of the application if necessary.
File Access Methods
There are two ways to access the file, and I’m not talking about reading and writing.
Sequential, which means that the data is stored in a specific order, and when you read/write to it, you read the data sequentially or the first piece of data, then the second, then the third, until you reach the end. If you are writing data, it gets placed at the end of the data.
Think of this are reading a novel. You read the first page, then the second, then the third. You cannot skip around and have the story make sense.
You also have direct access files, sometimes called random access files. In this case, you can jump to a location of a file. It is much faster in many ways, but a little more complicated.
Think of this like using your MP3 player. You can skip to a song you want to listen to. You don’t have to listen to the 10 songs that precede it.
An Intro to File IO in Python was originally found on Access 2 Learn