File handling
When we create and use arrayA set of data values of the same type, stored in a sequence in a computer program. Also known as a list. , variableA named storage location. The value of a variable can change, constants and tuples in our programming, the values held in these data stores are held in RAM (Random Access Memory).
As RAM is volatileA type of storage which contents are erased when the computer has been switched off or is interrupted., data will be lost when the computer is turned off. If we want to access the data from our programs, we will have to write the data to a fileAnything you save. It could be a document, a piece of music, a collection of data or something else. stored on secondary storageAdditional non-volatile storage for a computer, usually in the form of an external hard drive..
Reading from text files:
| Pseudocode | Explanation | |
| Reading one line | | Opens the file data called 'data.txt' and stores the contents in a variable 'myFile'. Reads the first line and stores in a variable called 'line'. Closes the file. |
| Reading all the lines | | Opens the file data and stores the contents in a variable 'myFile'. Reads all the lines in the file and stores in a variable called 'lines'. Closes the file. |
| Alternatively | | Using a 'with loop', only keeps the file open for the duration of the loop. This will prevent the file being locked, if required later. |
| Reading one line | |
|---|---|
| Pseudocode | |
| Explanation | Opens the file data called 'data.txt' and stores the contents in a variable 'myFile'. Reads the first line and stores in a variable called 'line'. Closes the file. |
| Reading all the lines | |
|---|---|
| Pseudocode | |
| Explanation | Opens the file data and stores the contents in a variable 'myFile'. Reads all the lines in the file and stores in a variable called 'lines'. Closes the file. |
| Alternatively | |
|---|---|
| Pseudocode | |
| Explanation | Using a 'with loop', only keeps the file open for the duration of the loop. This will prevent the file being locked, if required later. |
Writing to text files:
| Pseudocode | Explanation | |
| Writing a line to a file | | The file data is open and stored in the variable 'myFile'. The string "Hello World" is written to the file. The file is closed. |
| Alternatively | | File is open in write mode using a 'with loop'. A string is assigned to a variable called 'data'. The variable is written to the file. The file does not need to be closed. |
| Writing a line to a file | |
|---|---|
| Pseudocode | |
| Explanation | The file data is open and stored in the variable 'myFile'. The string "Hello World" is written to the file. The file is closed. |
| Alternatively | |
|---|---|
| Pseudocode | |
| Explanation | File is open in write mode using a 'with loop'. A string is assigned to a variable called 'data'. The variable is written to the file. The file does not need to be closed. |
There are three modes in which files can be opened. Read mode will allow you to read from a file, write mode will allow you to write to the file, and append mode will allow you to append the file (add to the end of it).