Increasingly Trivial Questions

First Hermit     Hello, are you a hermit by any chance?
Second Hermit     Yes that’s right. Are you a hermit?
First Hermit     Yes, I certainly am.
Second Hermit     Well I never. What are you getting away from?
First Hermit     Oh you know, the usual – people, chat, gossip, you know.
Second Hermit     Oh I certainly do – it was the same with me. I mean there comes a time when you realize there’s no good frittering your life away in idleness and trivial chit-chat.

If you remember back a few tutorials ago, we learnt how to pickle objects so that we could get them back later.  To pickle the object triviaQuestions (which was a list object) we first imported the pickle module, then opened a file and dumped the object into the file, then close()d the file:

->code snippet removed to note [1] because I don't want you typing it in<- 

Note here that when we opened the file with ‘w’ this meant we were ‘w’riting to it.  If the file existed, then Python wiped it ready for us to write something new to it. You need to be careful when reading from a file that you have ‘r’ (for ‘r’ead) in the open() command, otherwise, instead of reading the data, you’ll wipe it instead!!!!
Let’s load the triviaQuestions list:

>>> import cPickle
>>> filename = "p4kTriviaQuestions.txt"
>>> fileObject = open(filename,'r')  # note the 'r' for 'read'
>>> triviaQuestions = cPickle.load(fileObject)
>>> len(triviaQuestions)
>>> triviaQuestions[0]
['Who expects the Spanish Inquisition?', 'Nobody', 'Eric the Hallibut', 'An unladen swallow', 'Brian', 'Me!']
>>> triviaQuestions[1]
['What is the air-speed velocity of an unladen swallow?', 'What do you mean?  African or European swallow?', '10 m/s', '14.4 m/s', '23.6 m/s']

Wow! We have preserved the list between tutorials. So, rather than continually typing out all the questions every time, we only have to add new questions.

In this code we’ve imported cPickle. We mentioned cPickle earlier.  It does (for our purposes) the same thing as pickle, only it does it faster.  cPickle and pickle behave  little differently in some special circumstances, but that’s not relevant for us.  We also used ‘r’ rather than ‘w’ when we opened the file.  Also, cPickle doesn’t care about the name the object had when it was dump()ed.  We could have loaded the pickle into an object with a different name:

>>> fileObject.close()
>>> fileObject = open(filename,'r')
>>> aList = cPickle.load(fileObject)
>>> fileObject.close()
>>> aList == triviaQuestions

We had to first close() the fileObject so that cPickle read from the start of the file.  If there are multiple objects pickled to a file it loads them out in the order they were dumped in.  So once it load()s an object cPickle has moves to the end of the object in the file in order to read the next object.  If there is only one object, then cPickle will be at the end of the file and won’t be able to read any more.

As we can see, we’ve load()ed the same object under a different name (aList).  You should also note here that when an object is load()ed, it doesn’t change the file (unlike when an object is dump()ed).  We could load multiple copies of the object for ever if we wanted to.
Let’s add another question and then dump the list to the file:

sampleQuestion = []
# this clears the earlier entries
# if we append without doing this
# we'll have multiple questions in the wrong list
# first the question
sampleQuestion.append("Is this the right room for an argument?")
# then the correct answer
sampleQuestion.append("I've told you once.")
# now one or more incorrect answers
sampleQuestion.append("Down the hall, first on the left")

now we append() this question to the end of the list:


Finally, we dump the object back into the file.  Be careful – when we open the file to write (‘w’) we lose what’s saved in it.  That’s no problem if we fill it back up with data, so be sure to dump the question list!

>>> fileObject = open(filename,'w')  # data that was in the file is now gone!
>>> cPickle.dump(triviaQuestions,fileObject)   # ok, put new data in
>>> fileObject.close()

If you were worried about losing your data you would first dump() the object to a file with a temporary name, then when that was successful, rename the existing file as ‘p4kTriviaQuestions.bak’  and the newly created file to ‘p4kTriviaQuestions.txt’.  Then if anything went wrong with saving the data you would still have the data backed up in the “.bak” file.


  • add some more questions to your triviaQuestions list by load()ing the list from your file, adding questions, then dump()ing the list back to the file. Make sure you use ‘r’ when you open a file you are going to load() from and ‘w’ when you open a file to dump() to.


[1] This is repeated from the penultimate tute (or antepenultimate if you are counting this tute):

>>> import pickle  
>>> # This is just a flashback don't retype this code because it will wipe your data!!!!
>>> filename = "p4kTriviaQuestions.txt"
>>> fileObject = open(filename,'w')
>>> pickle.dump(triviaQuestions,fileObject)
>>> fileObject.close()


4 Responses to Increasingly Trivial Questions

  1. Pingback: Linux News » Python4Kids: New Tutorial – Increasingly Trivial Questions

  2. Naruhiko Ogasawara says:


    I have known your blog via openSUSE weekly news. I’m actually not a kid, but a newbie Python programmer, so your blog is really good for me. Thanks for your good contents.

    Anyway, in my environment, your code snippets can’t see right (it can’t be fold or scrolled). Could you check your CSS? I feel content#pre needs some option because it inherits “overflow:hidden” from content:div.

    I’m not special about this so please ask someone if you’ll fix it.

  3. brendanscott says:

    Konnichi wa Naruhiko san,

    The source code gets cut off because of the theme. I’ve had a look an chosen another theme. Hopefully this will be better

    • Naruhiko Ogasawara says:

      Thanks for awesome quick action!
      Now we can read all of your code well :). I’m grad to help you (a little bit).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

%d bloggers like this: