Skip to main content

Section 11.3 Reading a File

As an example, suppose we have a text file called ccdata.txt that contains the following data representing statistics about climate change. Although it would be possible to consider entering this data by hand each time it is used, you can imagine that it would be time-consuming and error-prone to do this. In addition, it is likely that there could be data from more sources and other years. The format of the data file is as follows:
Year, Global Average Temperature, Global Emmision of CO2
To open this file, we would call the open function. The variable, fileref, now holds a reference to the file object returned by open. When we are finished with the file, we can close it by using the close method. After the file is closed any further attempts to use fileref will result in an error.
>>>fileref = open("ccdata.txt", "r")
>>>
>>>fileref.close()
>>>
You have attempted of activities on this page.