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:

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.    

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

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.

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

Or you can just read one line.

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

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.

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.

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:

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

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.

Results in 

pythonXMLOutput.jpg - 11/2015

About CDB 360 Articles
Self-Employed Software Developer, Spark, Property Management, Hobby Forestry, Ex-Teacher, Engineering - Wood, Metal, Electrics & Computers. Outdoors - Walk, Cycle, Kitesurf,

Be the first to comment

Leave a Reply

Your email address will not be published.


*


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