Let's say that we want to read a file named "versedog.txt" from the current folder (Python always has a current folder -- it's the same story as when we had to import modules.) Here is how we can do it:

In [1]:
f = open("versedog.txt", encoding="latin1")

(To make this work, place your .py file in the same folder as the file that you want to read, and use "Run file as script" in Pyzo).

We specify encoding="latin1" to make Python interpret all characters in versedog.txt as English letters. This will avoid all sorts of problems with file encodings. Of course, if the file is not actually written in the Lating alphabet, we'd be in big trouble!

To get the text of the file, we can now use

In [2]:
s = f.read()

s is now a string:

In [3]:
s
Out[3]:
'Verse For A Certain Dog, Dorothy Parker\n\nSuch glorious faith as fills your limpid eyes, \nDear little friend of mine, I never knew. \nAll-innocent are you, and yet all-wise. \n(For Heaven\'s sake, stop worrying that shoe!) \nYou look about, and all you see is fair; \nThis mighty globe was made for you alone. \nOf all the thunderous ages, you\'re the heir. \n(Get off the pillow with that dirty bone!) \n\nA skeptic world you face with steady gaze; \nHigh in young pride you hold your noble head, \nGayly you meet the rush of roaring days. \n(Must you eat puppy biscuit on the bed?) \nLancelike your courage, gleaming swift and strong, \nYours the white rapture of a winged soul, \nYours is a spirit like a Mayday song. \n(God help you, if you break the goldfish bowl!) \n\n"Whatever is, is good" - your gracious creed. \nYou wear your joy of living like a crown. \nLove lights your simplest act, your every deed. \n(Drop it, I tell you- put that kitten down!) \nYou are God\'s kindliest gift of all - a friend. \nYour shining loyalty unflecked by doubt, \nYou ask but leave to follow to the end. \n(Couldn\'t you wait until I took you out?)\n'

We can print the string, and have all the "\n" interepreted as newlines:

In [4]:
print(s)
Verse For A Certain Dog, Dorothy Parker

Such glorious faith as fills your limpid eyes, 
Dear little friend of mine, I never knew. 
All-innocent are you, and yet all-wise. 
(For Heaven's sake, stop worrying that shoe!) 
You look about, and all you see is fair; 
This mighty globe was made for you alone. 
Of all the thunderous ages, you're the heir. 
(Get off the pillow with that dirty bone!) 

A skeptic world you face with steady gaze; 
High in young pride you hold your noble head, 
Gayly you meet the rush of roaring days. 
(Must you eat puppy biscuit on the bed?) 
Lancelike your courage, gleaming swift and strong, 
Yours the white rapture of a winged soul, 
Yours is a spirit like a Mayday song. 
(God help you, if you break the goldfish bowl!) 

"Whatever is, is good" - your gracious creed. 
You wear your joy of living like a crown. 
Love lights your simplest act, your every deed. 
(Drop it, I tell you- put that kitten down!) 
You are God's kindliest gift of all - a friend. 
Your shining loyalty unflecked by doubt, 
You ask but leave to follow to the end. 
(Couldn't you wait until I took you out?)

Finally, let's introduce Pythons str.split() function. s.split(sep) splits the string s into a list of strings which in s are separated by the string sep. For example:

In [5]:
s.split("\n")
Out[5]:
['Verse For A Certain Dog, Dorothy Parker',
 '',
 'Such glorious faith as fills your limpid eyes, ',
 'Dear little friend of mine, I never knew. ',
 'All-innocent are you, and yet all-wise. ',
 "(For Heaven's sake, stop worrying that shoe!) ",
 'You look about, and all you see is fair; ',
 'This mighty globe was made for you alone. ',
 "Of all the thunderous ages, you're the heir. ",
 '(Get off the pillow with that dirty bone!) ',
 '',
 'A skeptic world you face with steady gaze; ',
 'High in young pride you hold your noble head, ',
 'Gayly you meet the rush of roaring days. ',
 '(Must you eat puppy biscuit on the bed?) ',
 'Lancelike your courage, gleaming swift and strong, ',
 'Yours the white rapture of a winged soul, ',
 'Yours is a spirit like a Mayday song. ',
 '(God help you, if you break the goldfish bowl!) ',
 '',
 '"Whatever is, is good" - your gracious creed. ',
 'You wear your joy of living like a crown. ',
 'Love lights your simplest act, your every deed. ',
 '(Drop it, I tell you- put that kitten down!) ',
 "You are God's kindliest gift of all - a friend. ",
 'Your shining loyalty unflecked by doubt, ',
 'You ask but leave to follow to the end. ',
 "(Couldn't you wait until I took you out?)",
 '']

We can get line 11 in the file using

In [6]:
s.split("\n")[11]
Out[6]:
'A skeptic world you face with steady gaze; '