5.7. Reading a File¶

As an example, suppose we have a text file called olympics.txt that contains information about olympians across different years. The contents of the file are shown at the bottom of this page.

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.

Note

A common mistake is to get confused about whether you are providing a variable name or a string literal as an input to the open function. In the code above, “olympics.txt” is a string literal that should correspond to the name of a file on your computer. If you put something without quotes, like open(x, "r"), it will be treated as a variable name. In this example, x should be a variable that’s already been bound to a string value like “olympics.txt”.

Data file: olympics.txt
Name,Sex,Age,Team,Event,Medal
A Dijiang,M,24,China,Basketball,NA
A Lamusi,M,23,China,Judo,NA
Gunnar Nielsen Aaby,M,24,Denmark,Football,NA
Edgar Lindenau Aabye,M,34,Denmark/Sweden,Tug-Of-War,Gold
Christine Jacoba Aaftink,F,21,Netherlands,Speed Skating,NA
Christine Jacoba Aaftink,F,21,Netherlands,Speed Skating,NA
Christine Jacoba Aaftink,F,25,Netherlands,Speed Skating,NA
Christine Jacoba Aaftink,F,25,Netherlands,Speed Skating,NA
Christine Jacoba Aaftink,F,27,Netherlands,Speed Skating,NA
Christine Jacoba Aaftink,F,27,Netherlands,Speed Skating,NA
Per Knut Aaland,M,31,United States,Cross Country Skiing,NA
Per Knut Aaland,M,31,United States,Cross Country Skiing,NA
Per Knut Aaland,M,31,United States,Cross Country Skiing,NA
Per Knut Aaland,M,31,United States,Cross Country Skiing,NA
Per Knut Aaland,M,33,United States,Cross Country Skiing,NA
Per Knut Aaland,M,33,United States,Cross Country Skiing,NA
Per Knut Aaland,M,33,United States,Cross Country Skiing,NA
Per Knut Aaland,M,33,United States,Cross Country Skiing,NA
John Aalberg,M,31,United States,Cross Country Skiing,NA
John Aalberg,M,31,United States,Cross Country Skiing,NA
John Aalberg,M,31,United States,Cross Country Skiing,NA
John Aalberg,M,31,United States,Cross Country Skiing,NA
John Aalberg,M,33,United States,Cross Country Skiing,NA
John Aalberg,M,33,United States,Cross Country Skiing,NA
John Aalberg,M,33,United States,Cross Country Skiing,NA
John Aalberg,M,33,United States,Cross Country Skiing,NA
"Cornelia ""Cor"" Aalten (-Strannood)",F,18,Netherlands,Athletics,NA
"Cornelia ""Cor"" Aalten (-Strannood)",F,18,Netherlands,Athletics,NA
Antti Sami Aalto,M,26,Finland,Ice Hockey,NA
"Einar Ferdinand ""Einari"" Aalto",M,26,Finland,Swimming,NA
Jorma Ilmari Aalto,M,22,Finland,Cross Country Skiing,NA
Jyri Tapani Aalto,M,31,Finland,Badminton,NA
Minna Maarit Aalto,F,30,Finland,Sailing,NA
Minna Maarit Aalto,F,34,Finland,Sailing,NA
Pirjo Hannele Aalto (Mattila-),F,32,Finland,Biathlon,NA
Arvo Ossian Aaltonen,M,22,Finland,Swimming,NA
Arvo Ossian Aaltonen,M,22,Finland,Swimming,NA
Arvo Ossian Aaltonen,M,30,Finland,Swimming,Bronze
Arvo Ossian Aaltonen,M,30,Finland,Swimming,Bronze
Arvo Ossian Aaltonen,M,34,Finland,Swimming,NA
Juhamatti Tapio Aaltonen,M,28,Finland,Ice Hockey,Bronze
Paavo Johannes Aaltonen,M,28,Finland,Gymnastics,Bronze
Paavo Johannes Aaltonen,M,28,Finland,Gymnastics,Gold
Paavo Johannes Aaltonen,M,28,Finland,Gymnastics,NA
Paavo Johannes Aaltonen,M,28,Finland,Gymnastics,Gold
Paavo Johannes Aaltonen,M,28,Finland,Gymnastics,NA
Paavo Johannes Aaltonen,M,28,Finland,Gymnastics,NA
Paavo Johannes Aaltonen,M,28,Finland,Gymnastics,NA
Paavo Johannes Aaltonen,M,28,Finland,Gymnastics,Gold
Paavo Johannes Aaltonen,M,32,Finland,Gymnastics,NA
Paavo Johannes Aaltonen,M,32,Finland,Gymnastics,Bronze
Paavo Johannes Aaltonen,M,32,Finland,Gymnastics,NA
Paavo Johannes Aaltonen,M,32,Finland,Gymnastics,NA
Paavo Johannes Aaltonen,M,32,Finland,Gymnastics,NA
Paavo Johannes Aaltonen,M,32,Finland,Gymnastics,NA
Paavo Johannes Aaltonen,M,32,Finland,Gymnastics,NA
Paavo Johannes Aaltonen,M,32,Finland,Gymnastics,NA
Timo Antero Aaltonen,M,31,Finland,Athletics,NA
Win Valdemar Aaltonen,M,54,Finland,Art Competitions,NA
You have attempted of activities on this page