Sequential files

From Higher Computing Science
Jump to: navigation, search

This article is unfinished. Please consider joining and adding to this article. Read about Page layout beforehand.

Key points

  • Programs can access data from files. The process is the same in almost every programming language:
    • Open a file
    • For each line in the file:
      • Read line from file
    • Close file


  • Programs can also write data to files:
    • Open a file (create if file doesn’t exist)
    • For each item to be written to a file:
      • Write line to file
    • Close file
  • Multiple items can be read from each line, or written to one line of a file. Most files separate items using a comma.


Information

Reading and writing files is extremely important in programming. Without storing data to external files, it would not be possible to store data permanently. All programs that want to store data must write to a file or a database.

The program below is a Python program. It loads a CSV file into three arrays, then writes the entries with a score more than 50 back to another file.

Videos

Further information

Test yourself

Teaching resources