File I/O

Many programs require that we store data between runs or load at start up.  A simple example might be highest scores for a game or questions for a quiz program. Often the data will need to be structured in some way to make it easier to load and common examples include: Comma Separated Values (CSV);  EXtensible Markup Language (XML); and JavaScript Object Notation (JSON). Python makes it very easy to open and work with files.   All examples in this guide assume that the file is located alongside the program file in the same folder.

Quick Example

To open a file in the same folder as your program and convert it to a string use:

fileHandle = open ("theFilenameAndExtenjsion","r")
fileString =

Open (and closing) Files

Python has a very neat file open method that allows us to open a file for reading and writing.  When we use this command we specify a mode that tells Python what we intend to do e.g. r for reading, w for writing, a for appending and r+ for reading and writing.    

fileHandle = open (filename,"r")

In this example, I am opening a text file called rhymes.txt that is located in the same folder as my program for reading.

inputFile = open ("rhymes.txt","r")

In this second example I’m opening a file for writing but the file is located in a different folder so I have to give the full path.

outputFile = open ("H:Exportsoutput.txt","w")

When we have finished with any file handles we should close it and tell the computer we no longer need to hold this file in memory.


Reading from file handle

Once the file has opened you can interact with the file handle.  It’s worth reading the documentation if you really want to understand this. For example you can read the whole file into one string variable

var2 =

Or you can just read one line.

var1 = fileHandle.readline()

You can also loop through each line in the file one by one.

for line in fileHandle:
  print line

Be Aware:  As you read lines from the file your position in the file is updated.  In the following example var1 will only contain line 1 and var2 will contain lines 2 onwards even though it uses the read method.

var1 = fileHandle.readline()
var2 =

Writing to a file

You might want to write a line to the file.  This example opens a file and writes the current time to a file.

#Opening File
fileHandle = open (filename,"w")

#write the time to the file

Dealing with CSV

With very simple data structures such as a high scores table the CSV file format is perfect.   Each line of the file is arranged as values separated by commas (or in some instances semicolons) e.g. We can read files like this with the following code:

#Opening File
fileHandle = open (filename,"r")
#For each line in the file
for line in fileHandle:
score = line.split(",")
print score[0] + " scored " + score[1]

You can also append (or write – change the mode used) rows to a CSV file.

#Opening File
fileHandle = open (filename,"a")
playerName = "Arthur"
playerScore = 45
fileHandle.write(playerName + ", " + str(playerScore + "n") )

Dealing with XML

If you have more complicated data such as sprite information for a game then Extensible Markup Language (XML) might be a suitable approach.   XML data is structured in a similar style to HTML so if you have written some webpages in the past it should not look too scary. 

pythonXMLExample.jpg - 11/2015

The following code shows the first step in extracting information from an XML file.

#Open and parse the XML file.
 tree = ET.parse(filename)
#Find the root of the tree. root = tree.getroot()
#Loop through the children for child in root: print child.tag, child.attrib

Results in 

pythonXMLOutput.jpg - 11/2015

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.