Back to 课程

Computer Science GCES EDEXCEL

0% Complete
0/0 Steps
  1. Decomposition And Abstraction Edexcel
    2 主题
  2. Algorithms Edexcel
    11 主题
  3. Truth Tables Edexcel
    3 主题
  4. Binary Edexcel
    6 主题
  5. Data Representation Edexcel
    4 主题
  6. Data Storage And Compression Edexcel
    2 主题
  7. Hardware Edexcel
    5 主题
  8. Software Edexcel
    3 主题
  9. Programming Languages Edexcel
    2 主题
  10. Networks Edexcel
    7 主题
  11. Network Security Edexcel
    2 主题
  12. Environmental Issues Edexcel
    1 主题
  13. Ethical And Legal Issues Edexcel
    3 主题
  14. Cybersecurity Edexcel
    2 主题
  15. Develop Code Edexcel
    6 主题
  16. Constructs Edexcel
    4 主题
  17. Data Types And Data Structures Edexcel
    5 主题
  18. Operators Edexcel
    1 主题
  19. Subprograms Edexcel
    2 主题
课 Progress
0% Complete

Exam code:1CP2

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("Pollyn") # write line (n for new line)
file.write("Salesn")
file.write("26000n")
file.write("32n")

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

Examiner Tips and Tricks

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

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注