Understand serial files

From Computer Science Wiki
Python programming language[1]
  1. Serial files store data with no order to the data maintained.
  2. To search data from a serial file you begin at the start of the file and read all the data until the item is found.
  3. Data cannot be deleted from a serial file without creating a new file and copying all the data except the item you want to delete.
  4. Data cannot be changed in a serial file without creating a new file, copying all the data across to a new file, inserting the change at the appropriate point.
  5. Data can be appended to a serial file.
  6. A file can be open for reading or writing, but not reading and writing at the same time.
  7. Serial files are quite limiting, but are useful for simple data sets and configuration files. Other types of files include: sequential files where order of the data is maintained, index sequential files for large data sets and random files which allow you to access any item without searching through the file from the start.
 This content comes from our classroom resource for learning python

Challenge 1[edit]

Quote of the day challenge Using serial files, construct a program that outputs a 'quote of the day'

  1. open a text editor, and save a file "quotes.txt"
  2. enter in 3 quotes; push enter at the end of each line:
    1. Every dog has its day
    2. Penny wise pound foolish
    3. Every exit is an entrance to somewhere else
  3. save the file. Ensure the file is in the same folder as your python program.
import random

def random_line(f):
    lines = open(f).read().splitlines()
    return random.choice(lines)

print(random_line('quotes.txt'))

References[edit]