Learn Python in One Day and Learn It Well (2015)

Chapter 8: Working with Files

Cool! We’ve come to the last chapter of the book before the project. In this chapter, we’ll look at how to work with external files.

In Chapter 5 previously, we learned how to get input from users using the input() function. However, in some cases, getting users to enter data into our program may not be practical, especially if our program needs to work with large amounts of data. In cases like this, a more convenient way is to prepare the needed information as an external file and get our programs to read the information from the file. In this chapter, we are going to learn to do that. Ready?

Opening and Reading Text Files

The first type of file we are going to read from is a simple text file with multiple lines of text. To do that, let’s first create a text file with the following lines.

Learn Python in One Day and Learn It Well

Python for Beginners with Hands-on Project

The only book you need to start coding in Python immediately

http://www.learncodingfast.com/python
 

Save this text file as myfile.txt to your desktop. Next, fire up IDLE and type the code below. Save this code as fileOperation.py to your desktop too.

f = open (‘myfile.txt’, 'r')

firstline = f.readline()

secondline = f.readline()

print (firstline)

print (secondline)

f.close()

The first line in the code opens the file. Before we can read from any file, we have to open it (just like you need to open this ebook on your kindle device or app to read it). The open() function does that and requires two parameters:

The first parameter is the path to the file. If you did not save fileOperation.py and myfile.txt in the same folder (desktop in this case), you need to replace ‘myfile.txt’ with the actual path where you stored the text file. For instance, if you stored it in a folder named ‘PythonFiles’ in your C drive, you have to write ‘C:\\PythonFiles\\myfile.txt’ (with double backslash \\).

The second parameter is the mode. This specifies how the file will be used. The commonly used modes are

'r' mode:

For reading only.

'w' mode:

For writing only.

If the specified file does not exist, it will be created.

If the specified file exists, any existing data on the file will be erased.

'a' mode:

For appending.

If the specified file does not exist, it will be created.

If the specified file exist, any data written to the file is automatically added to the end

'r+' mode:

For both reading and writing.

After opening the file, the next statement firstline = f.readline() reads the first line in the file and assigns it to the variable firstline.

Each time the readline() function is called, it reads a new line from the file. In our program, readline() was called twice. Hence the first two lines will be read. When you run the program, you’ll get the output:

Learn Python in One Day and Learn It Well

Python for Beginners with Hands-on Project

You’ll notice that a line break is inserted after each line. This is because the readline() function adds the ‘\n’ characters to the end of each line. If you do not want the extra line between each line of text, you can do print(firstline, end = ‘’). This will remove the ‘\n’ characters. After reading and printing the first two lines, the last sentence f.close() closes the file. You should always close the file once you finish reading it to free up any system resources.

Using a For Loop to Read Text Files

In addition to using the readline() method above to read a text file, we can also use a for loop. In fact, the for loop is a more elegant and efficient way to read text files. The following program shows how this is done.

f = open (‘myfile.txt’, 'r')

for line in f:

print (line, end = ‘’)

f.close()

The for loop loops through the text file line by line. When you run it, you’ll get

Learn Python in One Day and Learn It Well

Python for Beginners with Hands-on Project

The only book you need to start coding in Python immediately

http://www.learncodingfast.com/python

Writing to a Text File

Now that we’ve learned how to open and read a file, let’s try writing to it. To do that, we’ll use the ‘a’ (append) mode. You can also use the ‘w’ mode, but you’ll erase all previous content in the file if the file already exists. Try running the following program.

f = open (‘myfile.txt’, 'a')

f.write(‘\nThis sentence will be appended.’)

f.write(‘\nPython is Fun!’)

f.close()

Here we use the write() function to append the two sentences ‘This sentence will be appended.’ and ‘Python is Fun!’ to the file, each starting on a new line since we used the escape characters ‘\n’. You’ll get

Learn Python in One Day and Learn It Well

Python for Beginners with Hands-on Project

The only book you need to start coding in Python immediately

http://www.learncodingfast.com/python

This sentence will be appended.

Python is Fun!

Opening and Reading Text Files by Buffer Size

Sometimes, we may want to read a file by buffer size so that our program does not use too much memory resources. To do that, we can use the read() function (instead of the readline() function) which allows us to specify the buffer size we want. Try the following program:

inputFile = open (‘myfile.txt’, 'r')

outputFile = open (‘myoutputfile.txt’, 'w')

msg = inputFile.read(10)

while len(msg):

outputFile.write(msg)

msg = inputFile.read(10)

inputFile.close()

outputFile.close()

First, we open two files, the inputFile.txt and outputFile.txt files for reading and writing respectively.

Next, we use the statement msg = inputFile.read(10) and a while loop to loop through the file 10 bytes at a time. The value 10 in the parenthesis tells the read() function to only read 10 bytes. The while condition while len(msg): checks the length of the variable msg. As long as the length is not zero, the loop will run.

Within the while loop, the statement outputFile.write(msg) writes the message to the output file. After writing the message, the statement msg = inputFile.read(10) reads the next 10 bytes and keeps doing it until the entire file is read. When that happens, the program closes both files.

When you run the program, a new file myoutputfile.txt will be created. When you open the file, you’ll notice that it has the same content as your input file myfile.txt. To prove that only 10 bytes is read at a time, you can change the lineoutputFile.write(msg) in the program to outputFile.write(msg + ‘\n’). Now run the program again. myoutputfile.txt now contains lines with at most 10 characters. Here’s a segment of what you’ll get.

Learn Pyth

on in One

Day and Le

arn It Wel

Opening, Reading and Writing Binary Files

Binary files refer to any file that contains non-text, such as image or video files. To work with binary files, we simply use the ‘rb’ or ‘wb’ mode. Copy a jpeg file onto your desktop and rename it myimage.jpg. Now edit the program above by changing the first two line lines

inputFile = open (‘myfile.txt’, 'r')

outputFile = open (‘myoutputfile.txt’, 'w')

to

inputFile = open (‘myimage.jpg’, 'rb')

outputFile = open (‘myoutputimage.jpg’, 'wb')

Make sure you also change the statement outputFile.write(msg + '\n') back to outputFile.write(msg).

Run the new program. You should have an additional image file named myoutputimage.jpg on your desktop. When you open the image file, it should look exactly like myimage.jpg.

Deleting and Renaming Files

Two other useful functions we need to learn when working with files are the remove() and rename() functions. These functions are available in the os module and have to be imported before we can use them.

The remove() function deletes a file. The syntax is remove(filename). For instance, to delete myfile.txt, we write remove(‘myfile.txt’).

The rename() function renames a file. The syntax is rename (old name, new name). To rename oldfile.txt to newfile.txt, we write rename(‘oldfile.txt’, ‘newfile.txt’).

Project: Math and BODMAS

Congratulations! We’ve now covered enough fundamentals of Python (and programming in general) to start coding our first full program. In this chapter, we’re going to code a program that tests our understanding of the BODMAS rule of arithmetic calculation. If you are unsure what BODMAS is, you can check out this site http://www.mathsisfun.com/operation-order-bodmas.html.

Our program will randomly set an arithmetic question for us to answer. If we get the answer wrong, the program will display the correct answer and ask if we want to try a new question. If we get it correct, the program will compliment us and ask if we want a new question. In addition, the program will keep track of our scores and save the scores in an external text file. After each question, we can key “-1” to terminate the program.

I’ve broken down the program into small exercises so that you can try coding the program yourself. Try the exercises before referring to the answers. Answers are provided in Appendix E or you can go to http://www.learncodingfast.com/python to download the Python files. I would strongly encourage you to download the source code as the formatting in Appendix E may result in the distortion of some indentation which makes the code difficult to read.

Remember, learning the Python syntax is easy but boring. Problem solving is where the fun lies. If you encounter difficulties when doing these exercises, try harder. This is where the reward is the greatest.

Ready? Let’s go!