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 ...
So that we can ...
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 ...
Your teacher will engage you in some activities to help you learn about these data security methods.
Where we learn about different ways that data can be kept secure Keeping data 'secure' means keeping it safe from ...
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.
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
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!
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!
Where we learn about the similarities and differences between secondary storage devices 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 ...
... 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 HDD - https://www.stellarinfo.co.in/kb/ssd-vs-hdd.php Where you investigate the operation of one secondary storage device. 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).
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'.
Answer the following questions
Before we start, review the following information ...
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. 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 ...
When calculating the raw file sizes for text files, you need to remember three important facts ...
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 ...
Image from Marienbad My Love website Where we learn to assess the suitability of different storage devices Features involved in choice of suitable storage
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
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 ...
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!
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 ...
Copy the table into your notebook under a suitable heading. Now choose and justify a suitable file mode for the following situations.
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 in your documents to hold the files for this task. Give it a suitable filename.
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. 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 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.
filename = 'poem.txt' file = '' handle = open(filename,'r') file = handle.read() handle.close() else: 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). 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?
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.
while True: chunk = handle.read(characters) if chunk == '': break else: file = file + chunk.replace('\n',' ') + '\n'
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).Type the following code in a new Python script (or replace the contents of the previous one).
import os file = handle.readlines() handle.close()
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='')
file = handle.readlines() handle.close()
print(line, end='') print(line.rstrip('\n'))
file = handle.readlines() handle.close()
print(line.rstrip('\n'))
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.Type the following code in a new Python script (or replace the contents of the previous one).
while True: line = handle.readline() if line == '': break else: file = file + line handle.close() ... or an easier way that doesn't actually involve using any file read instructions ...
for line in handle: file = file + line handle.close() 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.
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
Can you explain how the script works?
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 '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.
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.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. 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.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 ...
sequence = []
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. 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.
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.
Try drawing flowcharts to represent both of these different methods of writing lists to files. 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 ...
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 ...
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! 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.cs v file into a two dimensional list called films ...Programming challenges Attempt the following challenges to demonstrate your understanding.
Provide evidence of the scripts you have written.
Where we learn how to save binary files rather than text files
pickle which allows us to save almost anything that Python can throw at us into a file. Most Python data structures can be pickled ...
... 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.
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 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. 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. Finally (thank goodness!), if you are interested, you can download the amazing 'listpickler.py' script and be amazed!
I said you would be!
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. |