CS14 : Keeping data safe


https://docs.google.com/presentation/d/1DW8Ut9EtwgTcOGY6UF95Yl80ZtOIhqteTbibHbrH-DE/preview
Data security doesn't mean padlocking up your computer. It's all about how to keep the data safe from accidental loss. In this section, we'll consider the need for data security, the operation of various secondary storage devices and practical techniques for file handling.

We are learning ...
  • Why it is important to keep data safe
  • About the operation and suitability of secondary storage devices
  • Some practical file handling techniques
So that we can ...
  • Justify the need for data security
  • State examples of common optical, magnetic and solid state storage devices
  • Explain the principles of operation of common optical, magnetic and solid state storage devices
    - Online : hard disk drive (HDD), solid state drive (SSD)
    - Offline : digital versatile disk (DVD), compact disk (CD), blu-ray disc (BD), USB flash and removable disks
  • Discuss the meaning of and need for cloud storage and other contemporary storage devices
  • Discuss the security risks associated with different secondary storage technology
  • Justify suitable storage media for a given application
  • Calculate the storage requirements of a file
  • Practical file handling techniques
    - Text files (delimited-separated value files)
    - Binary files (pickle)
    - open, read, write, append, close operations

CGP The Revision Guide Page 6, 7, 51
CGP Exam Practice Workbook Page 9, 10, 57

# Get Ready.png

Activity 1 Data security  I   O   A   E 


We live our lives online these days - most, if not all, of our personal and business data is stored on devices and remote servers which wee trust will never be broken into and our precious information stolen. However, before we begin our 'learning', try the following quick activities ...
https://drive.google.com/file/d/0B83yXMOilskaR1JvY0hNZnhrU0k/preview
Click to engage

Your teacher will engage you in some activities to help you learn about these data security methods.


Task 1.1 Different types of security
Where we learn about different ways that data can be kept secure


Keeping data 'secure' means keeping it safe from ...
  • Corruption / human error
  • Malicious actions, unauthorised viewing, deleting, copying, corruption
In a presentation

Using your favourite presentation software, create an engaging learning activity with images, text, hyperlinks and interactivity to teach your peers about these 6 different data security methods. When you have finished, try out your learning activity on your shoulder partner.



Task 1.2 Consequences of data loss or corruption
Where we consider the consequences of data loss


Research

Visit the BBC news website and search for both data loss (generally accidental) and data theft (generally malicious). Read two or three of the articles - remember to focus on the stakeholders, the business and the customers.

Click the image to visit the news pages

In your notebooks / on paper

With your shoulder partner, discuss the consequences of data loss for a) Individuals and b) businesses. Think about the effects which data loss will have on these two stakeholder groups.

Although you may be asked to talk about what you've discussed!

Activity 2 Secondary storage devices  I   O   A   E 

This section is about secondary storage devices. Secondary storage is not directly accessible by the processor / CPU and therefore needs an I/O controller to access it. Secondary storage can be online or offline, as we shall see. 

Yes, yes, before you complain, I realise that you 'know' about these different storage devices ...

Non-Volatile Online and Offline Storage

They fall into three categories - optical, solid state and magnetic. Do you know why? Your teacher will show you examples of each type of storage device and explain how they operate and what they can be used for. All such storage devices are 'non-volatile' i.e. they retain their contents when the power is removed.

Some of you may be familiar with these but you don't need to learn about them now!


Task 2.1 Online, offline
Where we learn about the similarities and differences between secondary storage devices


In your notebooks / on paper

Identify and categorise the uses and nature of, analyse and evaluate the difference and similarities between online and offline storage. You may need to perform some research online to help you ...
... and should present your findings in a table with the following headings ...


If you are struggling, I've also prepared a very wordy and studentphobic 'Storage Devices Information Sheet' which you could download, if you wish. Compare your answers to your shoulder partner. Get your shoulder partner to peer assess your work, sign and date the response.


... but no-one uses floppy discs anymore!

OK - so we 'know' about the different online and offline storage devices, but how do they actually work? That's for you to find out. If you are lucky, your teacher will explain visually how the devices work before you embark on the next task.

Not a YouTube Logo - it's a copyright thing

NEW TASK - COMPARING SSD AND HDDhttps://www.stellarinfo.co.in/kb/ssd-vs-hdd.php


Task 2.2 Principles of operation
Where you investigate the operation of one secondary storage device.


Get your headphones on and spend a little (purposeful) time on YouTube looking for videos which explain how optical, magnetic and solid state storage devices work (Hint : click the links to begin!) Then ..

On a storyboard

Choose one of the three storage types and plan your own video. If you haven't got a storyboard template, ask your teacher for one or download your own from here.



Cloud refers to the provision of remote storage (in terms of location) which you access through the Internet either using a web browser or through a software application using an API (Application Programming Interface).


Task 2.3
 Cloud Storage
Where we learn about the features of common cloud storage platforms


Investigate

Investigate the following cloud storage providers are find out how much data they provide for free.
I've provided two links - one to the homepage so you can read about the provider and one to the 'pricing' page so you can see a) how much storage you get for free and b) how much you can pay for. Unless you really want to, I wouldn't suggest signing up to any of these, although most of you will have a Google Drive and / or an iCloud storage account already. It's worth noting that some providers give you extra stuff with your cloud storage subscription like synchronisation software and document editing capability so it's worth reading through the 'blurb'.

In your notebooks / on paper

Answer the following questions
  1. Rank the 7 cloud storage providers in increasing order of free storage.
  2. Why do you think that providers give free storage to personal customers?
  3. Describe three things that you could use these cloud storage platforms for.
  4. Which is your favourite cloud storage platform and why?

Activity 3 Storage requirements   I   O   A   E 

Before we start, review the following information ...

1 byte     (1B)  = the data storage required to store a single ASCII character
1 kilobyte (1kB) = 1024 bytes
1 megabyte (1MB) = 1024 kilobytes
1 gigabyte (1GB) = 1024 megabytes
1 terabyte (1TB) = 1024 gigabytes



Task 3.1 Calculating storage requirements for a file
Where we learn how to calculate the storage requirements for a file


First, we'll look at the file sizes of common things, including text, images and sounds.

On the worksheet

Download the worksheet 'Calculating storage requirements for a file' and complete the activity carefully (read the instructions). Check your answers with your teacher before you carry on. Then ...

In your notebooks / on paper

When calculating the raw file sizes for text files, you need to remember three important facts ...

FACT NUMBER 1 : One character of text occupies 1 byte of data storage.
FACT NUMBER 2 : Files always contain around 2% 'metadata' which stores details about the file itself.
FACT NUMBER 3 : Always try to quote the file size numerically between 1 and 1024 if possible

Calculate the raw file sizes for the following text files, giving your answers to 3 significant figures. I've done the first one for you as an example ...
  1. A single page text file containing 3000 characters (kB).

    Each character occupies 1 byte, so the whole page is 3000 bytes + 2% for metadata = 3060 bytes
    3060 bytes / 1024 = 3 kilobytes, so raw filesize = 3.00kB


  2. A chapter of a text book containing 13 pages, each with an average of 2700 characters per page (kB).
  3. A page in a novel containing 426 words. Each word contains, on average, 4 letters (including spaces) (kB).
  4. A 240 page research paper containing, on average, 73 lines of text per page with each line containing an average of 80 characters (including spaces) (MB).
  5. My 17-volume collection of 'Marienbad My Love by Mark Leach', the longest novel ever written weighing in at 10,710 pages, each page containing on average 1703 words averaging 6 characters long (including spaces) (MB)!

Image from Marienbad My Love website



Task 3.2 Justifying the choice of storage
Where we learn to assess the suitability of different storage devices


Features involved in choice of suitable storage

In your notebooks / on paper

For 6 (SIX) of the following scenarios, state the most suitable storage technology and justify your choice using the features stated in the figure above. Choose from ...

CD, DVD, Blu-ray, Internal Hard Disk, External Hard Disk, Magnetic Tape,
Solid State Drive (SSD), Secure Digital (SD) card, USB Flash Drive, Cloud Storage
  • Holding programs frequently used on a computer.
  • Storing school work so you can use it at home and at school.
  • Storing 3000 music tracks on a portable music player.
  • Using in your phone to store photos / videos.
  • Distributing 1.2GB of sales catalogue data to travelling sales staff through the post.
  • Storing the operating system for a low-end computer system.
  • Storing photos to share with your friends.
  • Archiving 350MB of data.
  • Storing the operating system on a high-end computer system.
  • Backing up files from your computer to store in a safe.
  • Taking to and from work with your files on.
  • Distributing software by post.
  • Recording video on a dedicated digital video recorder.
  • Backing up 160GB of programs and data held on a standalone home computer.
  • Watching a high definition 3D movie.
  • Watching a standard definition movie.
  • Backing up a server every day.



Activity 4 Practical file handling techniques   I   O   A   E 

We are used to manually saving and loading Python scripts we have written with the .py file extension on our internal and removable discs ...


But, we can also get the scripts themselves to save and load files which is very useful if we want to save and load data between script sessions. Think about the relationship between the Microsoft Word Application and the files it create, Word Documents. and you'll understand the relationship between the Python Script and it's Data Files ...


We can control the name and extension of the data files in Python easier than we can in other software application but we must still adhere to certain conventions ...
  • .txt stands for text file;
  • .pkl stands for pickle file.
  • .csv stands for comma separated value file;


Because, during file handling, the Python script and the data file interact, they need to be in either the same folder in your documents or in a folder relative to each other in the folder hierarchy. If you are not comfortable with folder structures and file paths, you need to speak to your teacher, pronto!



Task 4.1 Opening files
Where we learn a practical method for opening files


By opening a file in Python, you create a file handle which allows the script to locate the file on the computers disc drive. The file handle also allows the Python script to interact with the file through special methods.


ACTUALLY ... opening a file takes a copy of the file from the disk and loads it into the computers memory. All operations are carried out in the RAM. Only when the file is closed are the changes written back onto the disk. This is why you must always close the file and why you lose your work when your computer crashes!

[IMG - That's good to know!]

The syntax for opening a file is as follows ...

file_handle = open('filename',['mode'])

... where file_handle is a suitable identifier for the handle, 'filename' is the actual (relative) path and filename of the data file and the optional 'mode' (which defaults to 'r') can be one of the following choices ...

Different Python file modes

... where ...
  • 'r' stands for read - data is read from the file by the script;
  • 'w' stands for write - data is written to the file by the script, overwriting existing data;
  • 'a' stands for append - data is written to the file by the script, adding to the end of the existing data.

In your notebooks / on paper

Copy the table into your notebook under a suitable heading. Now choose and justify a suitable file mode for the following situations.
  1. A Python script written for an online retailer which adds new orders to the end of the list of existing ones;
  2. A Python script which reads a text file containing a novel which it then displays on the screen;
  3. A Python script which saves it's list of variables to a file before it closes.


Task 4.2
 On reading from files
Where we learn practical methods for reading data from files


Warning : This is a long task with lots of steps - there is no requirements to record anything until right at the end but you must carry out all the steps in order for you to learn!

The simplest thing we can do is to read from files that we have already created.


Create an empty folder

Create an empty folder in your documents to hold the files for this task. Give it a suitable filename.

Create a text file

Navigate to the folder and create an empty Notepad document (normally by right clicking your mouse and choose New > Text Document). Name the text document poem.txt, press the  ENTER  key to confirm the rename operation and double click the file to open it in Notepad. Type the following poem, pressing the  ENTER  key at the end of each line.

  There was an Old Man who supposed,
  That the street door was partially closed;
  But some very large rats,
  Ate his coats and his hats,
  While that futile old gentleman dozed.


Save the file and close it down. Important! The file won't update if you don't.

In a Python script

Now, create an empty Python script and save it in the same folder as the text file you have just created. Name the file reading.py and keep it open in the editor.


So, we've created our data file, in this case, a simple, but not very funny, poem. Once the file is opened, we can use on of three methods to read the file into our Python script. Also, we will check that the file exists before we open it - this prevents ugly error messages!


Read either the whole file or a portion of it

file.read([size])

This command reads some quantity of data from a file and return it as a string (in text mode) or as a byte object (in binary mode). The size parameter is optional. If it is blank, the whole file is read. If the end of the file is reached, file.read() returns an empty string, ''.


In a Python script

In your Python script, type the following carefully and save it. The working part is in blue but you need to type the whole script in for it to work. This script is essential.

import
os

filename = 'poem.txt'
file = ''

if os.path.isfile(filename):
 
handle = open(filename,'r')
  file = handle.read()
  handle.close()
else:
  print('File not found error')

if file:
  print(file)

Save and run the script by pressing the  F5  key. Your poem should appear on the screen! If you get any errors, check your code and make sure that the text file is in the same folder as the script (a very common error).

Check your understanding

Try to change the contents of the text file and run the script again. Try changing the filename of the file but leave the filename in the script the same and rerun the script - what happens?  


In a script

You can also read the file in 'chunks' - you'll see a possible application of this later on. Type the following code in a new Python script (or replace the contents of the previous one). Again, the working part of the script is in blue.

import
os

filename = 'poem.txt'
characters = 12
file = ''

if os.path.isfile(filename):
  handle = open(filename,'r')

  while True:
    chunk = handle.read(characters)
    if chunk == '':
      break
    else:
      file = file + chunk.replace('\n',' ') + '\n'

  handle.close()
else:
  print('File not found error')

if file:
  print(file)

Notice that I have replaced any newline characters ('\n') with a space (' ') so that I prevent the line breaks and allow the lines to flow. Again, save the file and run it by pressing the  F5  key. Can you explain what has happened? Try changing the value 12 to 15 to see how the output changes. This is an interesting example of a subroutine interface - we can change the parameters to alter the behaviour of the subroutine without altering the code behind it.

Read all the lines in the file at once

file.readlines()

This command reads the whole file into a list, with each line in the data file corresponding to a list item. However, the file.readlines() command preserves the newline characters ('\n') at the end of each line (which can be a problem).


In the script

Type the following code in a new Python script (or replace the contents of the previous one).

import os

filename = 'poem.txt'
file = ''

if os.path.isfile(filename):
  handle = open(filename,'r')

  file = handle.readlines()
  handle.close()
else:
  print('File not found error')

if file:
  for line in file:

    print(line)

Now save and run the script by pressing the  F5  key. Do you notice the difference between this and the last printed output? Yes, the blank lines - that's because Python keeps hold of the newline characters from the data file and adds an extra one from the print() statement, There are three ways of fixing this ...

Option 1 : Alter the last line in the program to ... print(line,end='')

import 
os

filename = 'poem.txt'
file = ''

if os.path.isfile(filename):
  handle = open(filename,'r')

  file = handle.readlines()
  handle.close()
else:
  print('File not found error')

if file:
  for line in file:

    print(line, end='')

Option 2
: Alter the last line in the program to ... print(line.rstrip('\n'))

import 
os

filename = 'poem.txt'
file = ''

if os.path.isfile(filename):
  handle = open(filename,'r')

  file = handle.readlines()
  handle.close()
else:
  print('File not found error')

if file:
  for line in file:

    print(line.rstrip('\n'))

Option 3
: Manually strip the newline characters from each item in the list before you return it ...

import 
os

filename = 'poem.txt'
file = ''

if os.path.isfile(filename):
  handle = open(filename,'r')

  file = handle.readlines()
  handle.close()
  for i in range(len(file)):
    file[i] = file[i].rstrip('\n')
else:
  print('File not found error')

if file:
  for line in file:

    print(line)

Whilst this last method seems complicated, it does give you a properly formatted list, which can't be bad. I would always prefer to use this method (and so should you!)

Read one line at a time

file.readline()

Finally, this command reads a single line from a file, leaving a newline character ('\n') at the end of the string, apart from at the end of the file if there isn't one (eh?) A blank line in the file is returned as a single newline ('\n'). If file.readline() returns an empty value, the end of the file has been reached.


In the Python script

Type the following code in a new Python script (or replace the contents of the previous one).

import
os

filename = 'poem.txt'
file = ''

if os.path.isfile(filename):
  handle = open(filename,'r')

  while True:
    line = handle.readline()
    if line == '':
      break
    else:
      file = file + line
  handle.close()
else:
  print('File not found error')

if file:
  print(file)

... or an easier way that doesn't actually involve using any file read instructions ... 

import
os

filename = 'poem.txt'
file = ''

if os.path.isfile(filename):
  file = ''
  handle = open(filename,'r')

  for line in handle:
    file = file + line
  handle.close()
else:
  print('File not found error')

if file:
  print(file)

Notice that in both these examples, you have to wait until you have completely finished 'dealing' with the file before you close it down which is not ideal - it's much better to read the whole file into a variable and close the file handle immediately. Now save and run the script by pressing the  F5  key.

Good question!

Programming challenges

Attempt the following challenges to demonstrate your understanding.
  • Create a Python script using IDLE (File > New File), save it as ... Task 4.2 Challenges.py
  • Use it to record your attempt at the following challenges.
  • When you have completed as many of the challenges as you can, evidence your work through a combination of screenshots and written explanations in a word processed document with a suitable header and footer. 
  • Remember to show the script and the output it generates.
EASY

Create a text file containing the names of your top 10 favourite TV programs or films. Use a suitable Python script to print out the contents of the text file to the screen. Using a combination of screenshots and written explanation, print out the contents of the script, the text file and the output generated.

MEDIUM

Create a text file containing a list of decimal numbers. Write a script which reads in the file and works out the sum of the numbers it contains and prints out the total. HINT : Read the file into a list, iterate through the list and add the numbers together. All the values in a text file are strings so don't forget to change the numbers to floats before you add them together!

HARD

Create a text file containing a list of single words. Your script should work out which is the longest word in the list and print it out to the screen.




Decrypt a secret message!

There is one other useful file handling command called file.seek() which allows you to skip through a file. Download the text file 'secret.txt' and the script 'decrypt.py' and save them both into the same folder in your documents. Open both files and inspect them. Now run the script, supplying the filename for the text file and a numerical 'decryption key' (any number greater than or equal to 1).

 Keep trying different keys until you find the secret message 

Demonstrate your understanding

Can you explain how the script works?




Task 4.3
 On writing to files
Where we learn practical methods for writing to files


When you are writing data to files, you need to ask yourself a number of questions about file modes ...


There are two options for writing data to files - file.write() which writes one line at a time (without a newline character) and file.writelines() which writes a list of strings to a file. It's also a good idea to learn how to check whether a file exists first before we write to it, so that's what we'll do!


Download the Python file

Download 'writing_individual.py' and save the script in a suitable place in your documents. Find the script, right click on it and choose 'Edit with IDLE'. This is what the script should look like.

import
 os

filename = input('Enter filename including extension : ')

if os.path.isfile(filename):
  choice = input('File exists, (a)ppend or (o)verwrite : ')
  if choice == 'o':
    handle = open(filename,'w+')
  else:
    handle = open(filename,'a+')
else:
  handle = open(filename, 'w+')

while True:
  line = input('Line to write (enter to end) : ')
  if line == '':
    break
  else:
    handle.write(line + '\n')


handle.close()

This script uses the os module to allow Python to check whether the file you specified already exists where it asks you whether you want to (a)ppend or (o)verwrite the file, then setting a suitable file mode. The next part asks for the lines to write to the file, expecting a single  ENTER  to signal the end of the list.

Add comments to the sections / lines in script to describe what each part does. Check with your teacher to make sure you don't have any misconceptions on what you have learnt.

Check your understanding

Now experiment with the file to create a shopping list stored in shopping.txt. Practice writing to a new file, closing the script, appending to it, live it, enjoy it, but beware - the text file will NOT update if it is open. So when you have finished looking at your text file, close it down again before you re-run the script.

Download the Python file

Now download the file 'writing_sequence.py' and save the script in a suitable place in your documents. Find the script, right click on it and chose 'Edit with IDLE'. This is what the script should look like ...

import
os


sequence = []

while True:

  line = input('Line to write (enter to end) : ')
  if line == '':
    break
  else:
    sequence.append(line + '\n')

filename = input('Enter filename including extension : ')

if os.path.isfile(filename):
  choice = input('File exists, (a)ppend or (o)verwrite : ')
  if choice == 'o':
    handle = open(filename,'w+')
  else:
    handle = open(filename,'a+')
else:
  handle = open(filename, 'w+')

handle.writelines(sequence)

handle.close()

Can you see the difference between this script and the first one? That's right - the file is created first in a list and then written all in one go to the file. This is probably more suitable for most situations.

Add comments to the sections / lines in script to describe what each part does. Check with your teacher to make sure you don't have any misconceptions on what you have learnt.

Check your understanding

Again, experiment with the script and make yourself a list of your favourite programming languages. However, beware - the text file will NOT update if you have it open, so make sure you close it down when you have finished inspecting it before you re-run the script.

In your notebooks / on paper

Document what you have done in the previous part of the activity. Make sure you have printouts of the two scripts, 'writing_individual.py' and 'writing_sequence.py' stuck in your books and that you have written an explanation, in your own words, about how they work.



Flowcharts, mate.

Try drawing flowcharts to represent both of these different methods of writing lists to files.



Task 4.4
 Comma Separated Challenge
Where we learn how to store records using comma separated value files


A CSV, or 'Comma Separated Value', file is a simple database structure. We'll learn about databases in a later unit, but this section will serve as a simple introduction to this wonderful world.

Structure of the CSV file, films.csv

You can handle CSV files using native file operations (read and write) but, to make life a little easier for you, I've provided you with a CSV library file which looks like this ...

import
os

def csv2sequence(filename):
  """ Drag CSV file into a list """
  if os.path.isfile(filename):
    sequence = []
    handle = open(filename,'r')
    for line in handle:
      sequence.append(line.rstrip('\n').split(','))
    handle.close()
    return sequence
  else:
    print('File does not exist')
    return False

def sequence2csv(sequence,filename):
  """ Drop list into a CSV file """
  handle = open(filename,'w')
  for line in sequence:
    for i in range(len(line)):
      line[i] = str(line[i])
    handle.write(','.join(line)+'\n')
  handle.close()
  
This library contains two functions which we can use in our own scripts ...
  • csv2sequence(filename)
    If you call this subroutine with a filename, it will return the contents of the CSV file as a two dimensional list. All the values in this list will be strings since text files can only contain strings.

  • sequence2csv(list,filename)
    If you call this subroutine with a list and a filename, it will write the contents of the sequence into the file. As part of this routine, any non-string values in the lists will be converted to strings.


Download the CSV library and create a new Python script which uses it

The cool thing about Python is that if we have a library of functions held in one script, we can import it into any new script we create and use the functions without having to retype them! Make sure that you have a copy of the 'csv.py' file in a folder in your documents and create a new Python script in the same folder called films.py.

Type the following into this script ...

import csv
films = csv.csv2sequence('films.csv')

... save it but DON'T run it yet because you haven't created the CSV file for it to read!

Download a prebuilt CSV file

Now download a copy of 'films.csv' and save it in the same folder as both the csv.py and the films.py scripts. You could run the films.py file now but it won't do a lot apart from read the films.csv file into a two dimensional list called films ...


Programming challenges

Attempt the following challenges to demonstrate your understanding.
  • Create a Python script using IDLE (File > New File), save it as ... Task 4.4 Challenges.py
  • Use it to record your attempt at the following challenges.
  • When you have completed as many of the challenges as you can, evidence your work through a combination of screenshots and written explanations in a word processed document with a suitable header and footer. 
  • Remember to show the script and the output it generates.
EASY

Print out the list of films like this ...

Film 001 : Final Dreamer (Action)
Film 002 : The Lonely Edge (Comedy)
Film 003 : Shards of Voyager (Action)
Film 004 : The Serpent's Slaves (Historical)
Film 005 : The Word of the Blade (Adventure)
Film 006 : Dream in the Legacy (Comedy)
Film 007 : Whispering Ship (Mystery)
Film 008 : The First Flames (Fantasy)
Film 009 : Hunter of Heart (Science fiction)
Film 010 : The Lover's Consort (Fantasy)
Film 011 : The Captive of the Boyfriend (Historical)
Film 012 : Touch in the Soaring (Science fiction)

MEDIUM

Print out only the films with the word 'the' in the title. Remember that sometimes, the word 'the' looks like the word 'The' ...

HARD

This is a chance to really show off your programming skills! Using a similar method to the medium challenge, write a script which ...
  • Asks for a filename for the CSV file
  • Opens the file using the CSV library into a list
  • Asks the user for a particular genre
  • Prints out only the films in that genre

In your notebooks / on paper

Provide evidence of the scripts you have written.



Task 4.5
 Pickle it up (Extension work)
Where we learn how to save binary files rather than text files


So, we've learnt about text files and CSV files (which are just comma separated text files). Lastly, for simpler (!) file handling, Python has a special built in library called pickle which allows us to save almost anything that Python can throw at us into a file. Most Python data structures can be pickled ...
  • None, True, False, integers, floating point numbers, strings, bytes
  • Tuples, lists, sets, and dictionaries containing picklable objects
  • Instances of classes (objects)
... without turning them into strings first!

Yes, you can!

Like all external modules, the pickle module needs to be imported before we can use it ...

import pickle

Pickling and unpickling only happen when you want to load data into an object and save an object to a file. There are only really two pickle methods that you need to know ...

pickle.dump(object,filehandle)

... saves an object in a pickle file referenced by its filehandle, and ...

object = pickle.load(filehandle)

... which loads the whole pickle file and stores it with an identifier object

In the following exercise, we will use the pickle library to store and retrieve a list in a pickle file but we could easily adapt this to store any picklable object if we desired.


In a Python script

Create a simple (!) Python script which performs the following operations

- Import the pickle library to give access to pickle functions;
- Either generate a random sequence of values or ask the user for a sequence of values;
- Ask the user for a filename (usually ending .pkl);
- Open a file for binary writing (mode 'wb');
- Use pickle.dump() to save the sequence into file.

Test your script

Test your script works and print it out for your folders. Don't forget to add comments to it to explain how it works. If you open the .pkl file using Notepad++, it's contents might look like this ...

A typical binary file

... so you might want to print this out as well.

In a Python script

Create another Python script which performs the following operations ...

- Import the pickle library to give access to pickle functions;
- Import the os library to give access to filesystem functions;
- Ask the user for the filename of the .pkl file they wish to load;
- Check that the file exists and the open the file for binary reading (mode 'rb');
- Use pickle.load() to load the contents of the .pkl file into a list;
- Display the contents of the list on the screen.

Again, test your script works and print out a copy for your folders, not forgetting the comments.


List Picker

Finally (thank goodness!), if you are interested, you can download the amazing 'listpickler.py' script and be amazed!

I said you would be!

Assessment Task (Homework)

Collect together all the scripts you have used during this topic and print them all out using Notepad++ (so they are in colour). On each script, write a quick explanation of what it does and why it might be useful to you in your programming tasks. Make sure the scripts are stuck into your notebook.

Grading rubric

MASTER :  You have printed the scripts out clearly using Notepad++ so they are in colour and have written a simple, clear explanation of their function plus why each one might be useful.
APPRENTICE You have printed out the script in colour and have attempted to explain what it does.
NOVICE : You have printed out the script in black and written the name of the script at the top.

Click to download revision cards
https://docs.google.com/document/d/1-rGx9Azqs_BCpjbyXyMYupOtnZHySXdN9b_l3Rjlgio/export?format=pdf
Remember to print them single sided

# Flash cards.png
Click to load key word list to help you make your own flash cards 

https://goo.gl/forms/dbWfArHyKZESd1Oz2
Try to get 5/5!


Hungry for more?

Download and complete the 'Wordsearch'! I know it's not 'educationally beneficial' but it's still fun!


If you are a coding legend, ask your teacher to give you copies of the structured versions (using functions) of all the scripts you have used in the reading and writing sections of this lesson. I imagine that you will want to spend a little time with them, investigating how they work. You'll be directed back to these when we look at the Break it down topic.


Frequently Asked Questions (FAQ)


Q : Are there any other scripts for file handling?
A : Not really - there are lots of variations on a theme but as long as you know how to open, append, write and close text, CSV and binary files using native file handling or built in libraries, you're pretty much done.

Q : What is the best way to revise?
A : There is no magic bullet I'm afraid - you need to work hard during the topic and then revisit the topic as many times as you can - your knowledge and understanding will build each time.