Back to 课程

Computer Science GCES OCR

0% Complete
0/0 Steps
  1. Cpu Architecture Performance And Embedded Systems Ocr
    5 主题
  2. Primary And Secondary Storage Ocr
    6 主题
  3. Data Storage And Compression Ocr
    12 主题
  4. Networks And Topologies Ocr
    6 主题
  5. Wired And Wireless Networks Protocols And Layers Ocr
    6 主题
  6. Identifying And Preventing Threats To Computer Systems And Networks Ocr
    2 主题
  7. Operating Systems And Utility Software Ocr
    2 主题
  8. Ethical Legal Cultural And Environmental Impact Ocr
    2 主题
  9. Computational Thinking Searching And Sorting Algorithms Ocr
    3 主题
  10. Designing Creating And Refining Algorithms Ocr
    5 主题
  11. Programming Fundamentals And Data Types Ocr
    5 主题
  12. Additional Programming Techniques Ocr
    7 主题
  13. Defensive Design And Testing Ocr
    6 主题
  14. Boolean Logic Diagrams Ocr
    2 主题
  15. Programming Languages And Integrated Development Environments Ides Ocr
    3 主题
  16. The Exam Papers Ocr
    2 主题
  17. Structuring Your Responses Ocr
    3 主题
课 Progress
0% Complete

Exam code:J277

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

OCR exam reference

Python

Open

file = open("fruit.txt")

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

Close

file.close()

file.close()

Read line

file.readline()

file.readline()

Write line

file.writeline("Oranges")

file.write("Oranges")

End of file

file.endOfFile()

endOfFile = False

Create a new file

newFile("Shopping.txt")

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

Append a file

n/a

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

Python example (reading data)

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 (writing new data)

Employees

 

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

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()

Guidance

  • 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

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