When computers are not reading code, they are processing data. Data is usually stored in a file. These tips show you how to do some common tasks related to files using Python.
Particularly when programming for the World Wide Web, it is frequently necessary to limit how much input data is read at a time. In this tip, I show how to read data from a file repeatedly, gradually reading and processing all of it.
When one has plenty of programming resources to spare, it is often easier to read all of the data from a file into a dictionary. This article shows you how.
In my experience, one seldom has the leisure of reading data from discrete sources. Very often, one needs to read data from multiple points in a file and evaluate the results. This tip shows how to do that.
Depending on the architecture of your program, you may need to read the same line from a file more than once or every time the program executes. If so, you should acquaint yourself with the module linecache
. Here I show you how to read a single line from the middle of a file, leaving the other lines untouched.