2.5. Python’s Input / Output Facilities

2.5.1. Basic I/O - printing

Text can be displayed on the screen using either the sys.stdout.write() function or the print operator. [1] Notice in the examples below that the comma (,) after the print causes it to not insert a linefeed into the output. sys.stdout.write() may seem redundant, which it is, but it is part of a larger family of I/O functions for reading and writing with stdout and also stdin an stderr.

>>> import sys
>>> for i in range(3):
...     sys.stdout.write("Hi ")
Hi Hi Hi

>>> for i in range(3):
...     print "Hi"

>>> for i in range(3):
...     print "Hi",
Hi Hi Hi

2.5.2. Basic I/O - reading data

The facility for getting numeric data in Python from the keyboard is the input() function, which also prints the prompt message to the user:

>>> x = input("Enter a number: ")
Enter a number: 5

>>> x

Notice in the following example, that input() evaluates the data entered and does not expect to receive a string unless the user puts quotes around the string:

>>> name = input("Enter your name: ")
Enter your name: Tim

Traceback (most recent call last):
File "<pyshell#47>", line 1,
NameError: name 'Tim' is not defined

>>> name = input("Enter your name: ")
Enter your name: 'Tim'

>>> name

To make it easier to read in a string, there also is a raw_input() function, which does not evaluate the data before returning it, which is sometimes what you want, and sometimes not:

>>> name = raw_input("Enter your name: ")
Enter your name: Tim

>>> name

>>> x = raw_input("Enter a number: ")
Enter a number: 3

>>> x


Change in Python 3: If you are using Python 3, please note that this has changed. The input() built-in function always returns a string. To convert the string to a number, either use eval(), or cast the string to the desired data type with int(), float() See the documentation for Python 3 built-in functions.

2.5.3. File I/O - reading a text file

Reading a text file:

>>> fileobj = open( "foo.txt", 'r' )
>>> lines = fileobj.readlines()
>>> lines
['Test file\n', 'line two\n', 'line three']

>>> fileobj.close()

Note that read (‘r’) is the default mode for the file open() function. So in the next example, the mode is left off. This time, let’s read the lines from the file one at a time:

import sys
fileobj = open("foo.txt")
lines = [] # an empty list
while 1:
    line = fileobj.readline()
    if line == "":
for l in lines:

2.5.4. File I/O - Writing a file

fileobject = open("bar", 'w' )
fileobject.write("Hello, World\n")

2.5.5. File I/O - Pickle your data

If you have some data in your program that you will want the next time you run your program, the cPickle module provides an easy way to save the data and load it when the program starts up again.

>>> d = {'one':1, 'two':2, 'three':3 }
>>> d
{'three': 3, 'two': 2, 'one': 1}

>>> import cPickle
>>> file = open("dpickle", 'w')
>>> cPickle.dump(d, file)
>>> file.close()

>>> fo = open("dpickle", 'r')
>>> newd = cPickle.load(fo)
>>> fo.close()
>>> newd
{'one': 1, 'three': 3, 'two': 2}


[1]In Python 3.0 and beyond, print is changed to a function. Python version 2.6 and beyond also support use of print() as a function.