Source: https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/2-2-1-file-handling

[Revision notes](https://senecalearning.com/en-gb/revision-notes.md) › [GCSE](https://senecalearning.com/en-gb/revision-notes/gcse.md) › [Coding](https://senecalearning.com/en-gb/revision-notes/gcse/coding.md) › [National Curriculum](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum.md) › File Handling

# File Handling

Coding: Introduction to Python v3, topic 2.2.1

## Files

Without files we can only store data in the memory.

### Files

![Files](https://image-v2.cdn.app.senecalearning.com/2018-07/4d7e1e31-f726-401e-92ad-d4e7fb3b49b2/File-Transfer-Files-Computer-.jpg)

- Files allow us to store data so that it persists even when our program is not running.
- This allows programs to 'carry on where they left off' when they are executed.

### Text files

![Text files](https://image-v2.cdn.app.senecalearning.com/2018-08/9cb6dab6-2b93-45ea-920a-64c300699210/Soviet-Constitution-Russian-Writing-.jpg)

- Text files consist of string data that can be read and processed by many applications.

### Binary files

![Binary files](https://image-v2.cdn.app.senecalearning.com/2018-07/31c77d00-497e-4e13-9319-bd43c2c1c731/Computer-Binary-1-0-Byte-.jpg)

- Binary files consist of binary data that is read and processed by specific applications.

## File Handling

We must be careful with the way that we access files so that we do not accidentally corrupt them.

### Open the file

![Open the file](https://image-v2.cdn.app.senecalearning.com/2020-08/c948165c-a8f0-4326-b157-de6e16db9e0d/door-open-entrance-open-door.jpg)

- First, we open the file for use, so that the program can read from or write to it.
- We can do this using:
 - open("filename.txt", "r")
 - open("filename.txt", "w")
- Opening with "r" will allow you to read from the file.
- Opening with "w" will allow you to write to the file

### Assign the file to a variable

![Assign the file to a variable](https://image-v2.cdn.app.senecalearning.com/2018-09/9e5843e2-42b9-4a41-8fa5-a2bf2240f09d/filing-cabinet-files-.png)

- The file must be assigned to a variable so that we can keep referring to it.
- This can be done as follows:
 - myFile = open("filename.txt", "r")
 - myFile = open("filename.txt", "w")

### Read / write

![Read / write](https://image-v2.cdn.app.senecalearning.com/2018-07/8a85e66f-79f5-4eec-b73c-ce40108d53b6/quran-reading-studying.jpg)

- We can now read or write to the file using the following commands:
 - myFile.readline() will return the next line of a file as a string.
 - myFile.write() will write a line to the file.

### Close the file

![Close the file](https://image-v2.cdn.app.senecalearning.com/2018-09/9daa7ad5-ad60-409e-984b-4a72612e8502/books.jpg)

- Finally, we must close the file so that it is free for another program to access.
- We can do this using:
 - myFile.close()

## Test yourself on File Handling

- What does a text file contain?
- What is the main reason to use files?
- File Handling
- Which of these are valid file handling methods?
- If we don't use the correct methods to access files, what might happen?

[Practise this topic for free](https://app.senecalearning.com/classroom/course/72d0364c-db0a-4630-8e0a-61f48cae0252/section/0fb21293-75c9-4177-8d0a-88c8658f2c61/session)

- Previous topic: [2.1.2 Strings](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/2-1-2-strings.md)
- Next topic: [3.1.1 Subroutines](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/3-1-1-subroutines.md)

## Coding: Introduction to Python v3 contents

### 1 Introduction to Python
- 1.1 Python
  - [1.1.1 Python](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-1-1-python.md)
- 1.2 Input & Output
  - [1.2.1 Input & Output](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-2-1-input-and-output.md)
- 1.3 Data Types
  - [1.3.1 Data Types](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-3-1-data-types.md)
- 1.4 Variables & Constants
  - [1.4.1 Variables & Constants](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-4-1-variables-and-constants.md)
- 1.5 Operators
  - [1.5.1 Arithmetic & Relational Operators](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-5-1-arithmetic-and-relational-operators.md)
  - [1.5.2 Boolean & Assignment Operators 2](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-5-2-boolean-and-assignment-operators-2.md)
- 1.6 Program Flow
  - [1.6.1 Selection](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-6-1-selection.md)
  - [1.6.2 Iteration](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/1-6-2-iteration.md)

### 2 Data Structures
- 2.1 Lists & Strings
  - [2.1.1 Lists](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/2-1-1-lists.md)
  - [2.1.2 Strings](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/2-1-2-strings.md)
- 2.2 File Handling
  - [2.2.1 File Handling](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/2-2-1-file-handling.md)

### 3 Modularity
- 3.1 Subroutines
  - [3.1.1 Subroutines](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/3-1-1-subroutines.md)
- 3.2 Data Flow
  - [3.2.1 Data Flow](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/3-2-1-data-flow.md)

### 4 Good Practice
- 4.1 Naming & Whitespace
  - [4.1.1 Identifiers](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/4-1-1-identifiers.md)
  - [4.1.2 Whitespace](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/4-1-2-whitespace.md)
- 4.2 Comments
  - [4.2.1 Comments](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/4-2-1-comments.md)
- 4.3 Exceptions
  - [4.3.1 Exceptions](https://senecalearning.com/en-gb/revision-notes/gcse/coding/national-curriculum/4-3-1-exceptions.md)
