Open sequential text files
Read from sequential text files
Write to sequential text files
Close text files
You will have to save data at some point in your program.
There are various ways to do this:
Files
In the Registry
In Databases
At Higher we will concentrate only on text/csv files. There are various methods to access files in Python but we will be using the methods in the File Object.
When a file is opened you have to specify how it should be opened. This is what type of access you will be using. When opening a file we can set the following modes:
Reading (this is the default and opens the file in a read only mode)
Writing (will usually overwrite the file)
Appending (will add to the end of the existing contents)
Some other Python File Access Modes
r - Open for reading (this is the default)
w - Open for writing, truncating the file first
a - Open for writing, appending to the end of the file if it exists
Assuming that the file exists in the same folder as the Python Program the pseudocode below shows opening a file for reading and reading all of the contents into a variable called filecontents.
OPEN FILE(SAMPLEFILE.txt) FOR READING
filecontents = FILE.READ
CLOSE FILE
The code below would open the file for reading. Again assuming that the file sampleFile.txt is in the same folder. When the file has been read into the variable called filecontents
Once the with loop ends the file is closed. Remember to put Close File in pseudocode.
There are different ways to read the contents of a file:
ReadLine - Reads a single line of characters from the file and returns the data as a string.
Read - Reads all characters from the current position to the end of the file.
A CSV file is simply a plain text file where each piece of data is separated by a comma. And each new record is on a new line e.g. below:
Mark,43
Joanna,54
It is important for you to analyse the file structure as this will dictate how the file is to be read and processed. The order of the pieces of data is important. Whether a file extension is .csv or plain text .txt as long as is comma seperated you should process the file in the same way.
Line 1 Imports the csv library
Line 5 Opens the file
Lines 6 and 7 Counts the number of lines in the file and then resets the position in the file back to the beginning
Lines 8-10 Declares the parallel arrays with the appropriate number of elements
Line 11 Reads the file and declares a file object
Line 12 Is a loop that will iterate through every row in the file. the Row object will contain an array that will contain each piece of data that was seperated by a comma
Lines 13-15 will place the date in each element of the row array into the appropriate parallel arrays
Line 16 increases the counter so that the next data will be places in the next element of the parallel arrays
Line 18 returns the three parallel arrays
This method means that both the array(s) and the loop can cope with a file of any size.
The slides below will go through the reading and process of a line of a CSV file into parallel arrays in more detail.
In the most basic method of writing to a file you write a string at a time, and when needed will put a new line character at the end so that the next string will be on a new line.
OPEN FILE("newfile.txt" for WRITING)
WRITE Python is great fun" TO FILE
WRITE "This text will be on another line" TO FILE
CLOSE FILE
When you need to create a CSV file from either variables, arrays or records you will need to create a single line of a file at one time. You will notice in the example below that we add the \n character at the end. This is Python's End of Line character so that the next writeline operation will take place on a new line.
The code below would take in an array of forenames, surnames and Scottish Candidate Numbers and will write them to a CSV file