Python Open Function – Read Text Files

Text files are stored in secondary memory, so you need a way that tells Python where to go look for the text file we want it to read. This is what the built-in, Python open function is for.

For example:

handle = open(readme.txt, r)

In this example we assigned a variable name of handle that we can use to manipulate the file. The first parameter inside the parenthesis is the actual file name we want to open, and the second parameter of r indicates read mode. The other option is w for write mode, but for now we will use the read mode. The mode r is chosen by default for the Python open function if left blank.

It is important to note that the variable handle is not the file itself. Rather, it is a mechanism to use the file. To keep things simple, you want your text file to be in the same folder as your Python code file.

Before moving forward, you have to understand there is a special character to use that indicates when a line ends.

The New Line Character

>>>print ‘XY’
XY
>>>print ‘X\nY’
X
Y

The \n character tells us we start on a new line.

>>len(‘X\nY’)
3

You see that the new line character counts as only one character, even though it is technically two. Think of \n as syntax to encode a new line in a string.

You have to mentally visualize the new line character as being there at the end of lines in text files. Text editors do not show us this character when we are just looking at the text, but they are encoded there, and you have to know that.

The Efficiency of the Python Open Function

You can print every line in a file with three lines of Python code.

xfile = open(readme.txt)
for line in xfile:
++++print line

You can count the lines in a file with six lines of Python code.

xfile = open(readme.txt)
count = 0
for line in xfile:
++++count = count + 1
print ‘Line Count=’, count

Notice in these examples we used xfile instead of handle as our variable name. That is okay, it is just a variable name.

You can also read every character of a text file into one string. You would use the built-in read function.

xfile = open(readme.txt)
one_string = xfile.read()
print len(one_string)

This program will print the number of characters in your text file. In other words, it prints the length of one string.

You could print the first 20 characters.

print one_string[:20]

Select Only the Text You Want

Suppose you have a fairly large text file, and you only want to print only lines that start with the word ‘Time’.

xfile = open(readme.txt)
for line in xfile:
++++line = line.rstrip()
++++if line.startwith(‘Time’):
++++++++print line

You might be thinking, what is the purpose of line = line.rstrip()? Well, this bit of code strips the newline character from the text file. It is a built-in function that removes the whitespace so your program will not print blank lines.

You can skip a line by using the continue statement. The following Python code will skip every line that does not start with ‘I’.

xfile = open(readme.txt)
for line in xfile:
++++line = line.rstrip()
++++if not line.startwith(‘I’):
++++++++continue
++++print line

You can use ‘in’ to select lines. The following Python code will only select lines containing the ‘$’ character:

xfile = open(readme.txt)
for line in xfile:
line = line.rstrip()
++++if not ‘$’ in line:
++++++++continue
++++print line

How to Catch Bad User Input

Use this code to prompt a user for a file. This will catch a bad file if the user does not enter the file name correctly, or if the file does not exist.

xname = raw_input(‘Enter the file name: ‘)
try:
++++xfile = open(xname)
except:
++++print ‘File cannot be opened’
++++exit()

Leave a Reply

Your email address will not be published. Required fields are marked *