5.8. Alternative File Reading Methods¶
Once you have a file “object”, the thing returned by the open function, Python provides three methods to read data
from that object. The read()
method returns the entire contents of the file as a single string (or just the first n
characters if you provide a number as an input parameter). The readlines
method returns the entire contents of
the file as a list of strings, where each item in the list is one line of the file. The readline
method reads one line from the file and returns it as a string. The strings returned by readlines
or
readline
will contain the newline character at the end. Table 2 summarizes these
methods and the following session shows them in action.
Method Name |
Use |
Explanation |
---|---|---|
|
|
Read and return a string of |
|
|
Read and return the next line of the file with
all text up to and including the
newline character. If |
|
|
Returns a list of strings, each
representing a single line of the file.
If |
In this course, we will generally either iterate through the lines returned by readlines()
with a for loop,
or use read()
to get all of the contents as a single string.
In other programming languages, where they don’t have the convenient for loop method of going through the lines
of the file one by one, they use a different pattern which requires a different kind of loop, the while
loop.
Fortunately, you don’t need to learn this other pattern, and we will put off consideration of while
loops
until later in this course. We don’t need them for handling data from files.
Note
A common error that novice programmers make is not realizing that all these ways of reading the file contents, use up the file. After you call readlines(), if you call it again you’ll get an empty list.
The example below shows how to use the readline() method from above and demonstrates how calling this repeatedly gets a different line each time, as the file is used up.
Check your Understanding
Writing essays for school can be difficult but many students find that by researching their topic that they have more to say and are better informed. Here at the university we require many undergraduate students to take a first year writing requirement so that they can have a solid foundation for their writing skills. This comes in handy for many students. Different schools have different requirements, but everyone uses writing at some point in their academic career, be it essays, research papers, technical write ups, or scripts.
Using the file
school_prompt2.txt
, find the number of characters in the file and assign that value to the variablenum_char
.
This summer I will be travelling. I will go to... Italy: Rome Greece: Athens England: London, Manchester France: Paris, Nice, Lyon Spain: Madrid, Barcelona, Granada Austria: Vienna I will probably not even want to come back! However, I wonder how I will get by with all the different languages. I only know English!
Find the number of lines in the file,
travel_plans2.txt
, and assign it to the variablenum_lines
.
Sad upset blue down melancholy somber bitter troubled Angry mad enraged irate irritable wrathful outraged infuriated Happy cheerful content elated joyous delighted lively glad Confused disoriented puzzled perplexed dazed befuddled Excited eager thrilled delighted Scared afraid fearful panicked terrified petrified startled Nervous anxious jittery jumpy tense uneasy apprehensive
Create a string called
first_forty
that is comprised of the first 40 characters ofemotion_words2.txt
.
This summer I will be travelling. I will go to... Italy: Rome Greece: Athens England: London, Manchester France: Paris, Nice, Lyon Spain: Madrid, Barcelona, Granada Austria: Vienna I will probably not even want to come back! However, I wonder how I will get by with all the different languages. I only know English!
Writing essays for school can be difficult but many students find that by researching their topic that they have more to say and are better informed. Here are the university we require many undergraduate students to take a first year writing requirement so that they can have a solid foundation for their writing skills. This comes in handy for many students. Different schools have different requirements, but everyone uses writing at some point in their academic career, be it essays, research papers, technical write ups, or scripts.
Sad upset blue down melancholy somber bitter troubled Angry mad enraged irate irritable wrathful outraged infuriated Happy cheerful content elated joyous delighted lively glad Confused disoriented puzzled perplexed dazed befuddled Excited eager thrilled delighted Scared afraid fearful panicked terrified petrified startled Nervous anxious jittery jumpy tense uneasy apprehensive
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,25,Netherlands,Speed Skating,NA Christine Jacoba Aaftink,F,25,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,33,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 "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 Timo Antero Aaltonen,M,31,Finland,Athletics,NA Win Valdemar Aaltonen,M,54,Finland,Art Competitions,NA