<< Back to man.ChinaUnix.net

A Byte of Python

Chapter 12. Input/Output

Table of Contents

Files
Using file
Pickle
Pickling and Unpickling
Summary

There will be lots of times when you want your program to interact with the user (which could be yourself). You would want to take input from the user and then print some results back. We can achieve this using the raw_input and print statements respectively. For output, we can also use the various methods of the str (string) class. For example, you can use the rjust method to get a string which is right justified to a specified width. See help(str) for more details.

Another common type of input/output is dealing with files. The ability to create, read and write files is essential to many programs and we will explore this aspect in this chapter.

Files

You can open and use files for reading or writing by creating an object of the file class and using its read, readline or write methods appropriately to read from or write to the file. The ability to read or write to the file depends on the mode you have specified for the file opening. Then finally, when you are finished with the file, you call the close method to tell Python that we are done using the file.

Using file

Example 12.1. Using files

				
#!/usr/bin/python
# Filename: using_file.py

poem = '''\
Programming is fun
When the work is done
if you wanna make your work also fun:
	use Python!
'''

f = file('poem.txt', 'w') # open for 'w'riting
f.write(poem) # write text to file
f.close() # close the file

f = file('poem.txt') # if no mode is specified, 'r'ead mode is assumed by default
while True:
	line = f.readline()
	if len(line) == 0: # Zero length indicates EOF
		break
	print line, # Notice comma to avoid automatic newline added by Python
f.close() # close the file
				
				

Output

				
$ python using_file.py
Programming is fun
When the work is done
if you wanna make your work also fun:
        use Python!
				
				

How It Works

First, we create an instance of the file class by specifying the name of the file and the mode in which we want to open the file. The mode can be a read mode ('r'), write mode ('w') or append mode ('a'). There are actually many more modes available and help(file) will give you more details about them.

We first open the file in write mode and use the write method of the file class to write to the file and then we finally close the file.

Next, we open the same file again for reading. If we don't specify a mode, then the read mode is the default one. We read in each line of the file using the readline method, in a loop. This method returns a complete line including the newline character at the end of the line. So, when an empty string is returned, it indicates that the end of the file has been reached and we stop the loop.

Notice that we use a comma with the print statement to suppress the automatic newline that the print statement adds because the line that is read from the file already ends with a newline character. Then, we finally close the file.

Now, see the contents of the poem.txt file to confirm that the program has indeed worked properly.