Programming File Handling (Edexcel GCSE Computer Science)

Revision Note

James Woodhouse

Expertise

Computer Science

Programming File Handling

What is file handling?

  • File handling is the use of programming techniques to work with information stored in text files

  • Examples of file handing techniques are:

    • opening text files

    • reading text files

    • writing text files

    • closing text files

Concept

Python

Open

file = open("fruit.txt","r")

Close

file.close()

Read line

file.readline()

Write line

file.write("Oranges")

End of file

endOfFile = False

Create a new file

file = open("shopping.txt","w")

Append a file

file = open("shopping.txt","a")

  • The same approach for opening and writing to comma separated value files (CSV) can be used

  • When opening a CSV file, the file extension would change to ".csv"

    • file = open("fruit.csv","r")

What are the differences when file handling?

Concept

What it means

Open

  • Opens the file in memory to be used in the program

Close

  • Closes the file

  • This must be done to store any content that has been written or appended to it

Read

  • Opens the file in read-only mode

  • The contents can be read but not changed

Write

  • Opens the file in write mode

  • The contents of the file will be over-written

  • No prior content in the file will be saved

Append a file

  • Opens the file in append mode

  • New data will be added to the end of the file

  • Previous data in the file will remain

Python example (reading data from a text file)

Employees

Text file

file = open("employees.txt", "r") # open file in read mode
endOfFile = False # set end of file to false
while not endOfFile: # while not end of file
  name = file.readline() # read line 1
  department = file.readline() # read line 2
  salary = file.readline() # read line 3
  age = file.readline() # read line 4

  print("Name: ", name) # print name
  print("Department: ", department) # print department
  print("Salaray: ", salary) # print salary
  print("age: ", age) # print age
  
  if name == "": # if name is empty
    endOfFile = True # set end of file to true

file.close() # close file

Greg
Sales
39000
43
Lucy
Human resources
26750
28
Jordan
Payroll
45000
31

Python example (reading data from a csv file)

Employees

CSV file

import csv

# Open the CSV file

with open('employees.csv', 'r') as csv_file:

csv_reader = csv.reader(csv_file)

# Iterate over each row in the CSV file

for row in csv_reader:

print(row)

Greg, Sales, 39000, 43
Lucy, Human resources, 26750, 28
Jordan, Payroll, 45000, 31

Python example (writing new data to a text file)

Employees

 Text file

file = open("employees.txt", "a") # open file in append mode
file.write("Polly\n") # write line (\n for new line)
file.write("Sales\n")
file.write("26000\n")
file.write("32\n")

file.close() # close file

Greg
Sales
39000
43
Lucy
Human resources
26750
28
Jordan
Payroll
45000
31
Polly
Sales
26000
32

Python example (writing new data to a csv file)

Employees

CSV file

import csv

# Sample data to write to CSV

employees_data = [

['John', 'Sales', 50000, 30], ['Jane', 'Marketing', 60000, 35], ['Alice', 'Engineering', 70000, 40]

]

# Write data to CSV file

with open('employees.csv', 'w', newline='') as csv_file:

csv_writer = csv.writer(csv_file)

csv_writer.writerows(employees_data)

print("Data has been written to employees.csv")

John, Sales, 50000, 30,

Jane, Marketing, 60000, 35

Alice, Engineering, 70000, 40

Exam Tip

When opening files it is really important to make sure you use the correct letter in the open command

  • "r" is for reading from a file only

  • "w" is for writing to a new file, if the file does not exist it will be created. If a file with the same name exists the contents will be overwritten

  • "a" is for writing to the end of an existing file only

Always make a backup of text files you are working with, one mistake and you can lose the contents!

Worked Example

Use pseudocode to write an algorithm that does the following :

  • Inputs the title and year of a book from the user.

  • Permanently stores the book title and year to the existing text file books.txt [4]

How to answer this question

  • Write two input statements (title and year of book)

  • Open the file

  • Write inputs to file

  • Close the file

Example answer

title = input("Enter title")

year = input("Enter year")

file = open("books.txt")

file.writeline(title)

file.writeline(year)

file.close()

Marks

  • title = input("Enter title") 1 mark for both

    • year = input("Enter year")

  • file = open("books.txt") 1 mark

  • file.writeline(title) 1 mark for both

    • file.writeline(year)

  • file.close() 1 mark

You've read 0 of your 0 free revision notes

Get unlimited access

to absolutely everything:

  • Downloadable PDFs
  • Unlimited Revision Notes
  • Topic Questions
  • Past Papers
  • Model Answers
  • Videos (Maths and Science)

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

James Woodhouse

Author: James Woodhouse

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.