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 2[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.
# we start by importing the random library so we can call random methods
import random

# the line below creates a function named random_line. The function accepts a parameter called the_quote_file
def random_line(the_quote_file):

# the line below opens the file which was passed into the function and assigns the file object to a variable named lines.
    lines = open(the_quote_file).read()

# the line below calls the splitlines() method. splitlines() changes a string into a list. Each line break is a new element in the list.  
    lines = lines.splitlines()

# finally, we return a random element from the list named "lines". 
    return random.choice(lines)

# the line below we call the function "random_line" and pass that function a file named 'quotes.txt'. We then print the output.
print(random_line('quotes.txt'))


Challenge 3[edit]

Product catalogue challenge

A product has a code, description and price. Write a program that asks the user for the data for a product, and saves the data in a serial file.

# the lines below asks a user to enter product data, and assigns the input to variables
product_code = input("Enter product code: ")
product_price = input("Enter product price: ")
product_description = input("Enter product description: ")

# the lines below opens a file for WRITING (note the w) and assigns that file to a variable named catalog
catalog = open("product_catalog.txt", "w")

# the line below writes the variables "product_code", "product_price", and "product_description" to the file
# at the end of each line, we add a + "\n" <-- that creates a line break, just like pressing the enter key.
catalog.write(product_code + "\n")
catalog.write(product_price + "\n")
catalog.write(product_description + "\n")

# the line below closes the file:
catalog.close()

Challenge 4[edit]

Modify the program to continue to ask for products until the user does not enter a product code. All products should be saved in the file.

# the line below opens a file for WRITING (note the w) and assigns that file to a variable named catalog
# We need to open the file outside of the loop because everytime we open a file it overwrites everything in the file.

catalog = open("product_catalog.txt", "w")

# the below opens a loop. We have no idea how many products might be assigned, so we use a conditional
# loop. This is like saying "run until we specifically tell you to stop."

while True:
    # the lines below asks a user to enter a product code, and assigns the input to a variable
    product_code = input("Enter product code: ")

    # we have a special condition here. If code is blank, we need to end our program using the break keyword.
    if product_code == "":
        break
    # we continue to gather data for the new product here:
    product_price = input("Enter product price: ")
    product_description = input("Enter product description: ")


    # the lines below write the variables "product_code", "product_price", and "product_description" to the file
    # at the end of each line, we add a + "\n" <-- that creates a line break, just like pressing the enter key.
    catalog.write(product_code + "\n")
    catalog.write(product_price + "\n")
    catalog.write(product_description + "\n")


# the line below closes the file:
catalog.close()

Challenge 5[edit]

Create a simple menu that allows the user to enter a new catalogue, one new product to add to the end of the catalogue, or to output the items in a catalogue.

# the below opens a loop. We have no idea what a user might choose, so we use a conditional
# loop. This is like saying "run until we specifically tell you to stop."

while True:
    # the lines below print our menu:
    print("1. create a completely new catalog of products, erasing the old catalog")
    print("2. add a product to an existing catalog")
    print("3. see the catalog")
    print("4. quit")
    print("="*30)
    
    # the line below asks the user for their choice and assigns the choice to a variable named "choice".
    choice = int(input("Enter your choice and press enter: "))
    
    if choice == 1:
        # we are starting from scratch here, so we open the file for writing:
        catalog = open("product_catalog.txt", "w")
        
        # we don't know how products a user needs to enter, so we use this conditional loop: 
        while True:

            # the lines below asks a user to enter a product code, and assigns the input to a variable
            product_code = input("Enter product code: ")

            # we have a special condition here. If code is blank, we need to end our program using the break keyword.
            if product_code == "":
                break
            # we continue to gather data for the new product here:
            product_price = input("Enter product price: ")
            product_description = input("Enter product description: ")


            # the lines below write the variables "product_code", "product_price", and "product_description" to the file
            # at the end of each line, we add a + "\n" <-- that creates a line break, just like pressing the enter key.
            catalog.write(product_code + "\n")
            catalog.write(product_price + "\n")
            catalog.write(product_description + "\n")

        # the line below closes the file:
        catalog.close()

    # choice 2 is to append one single product
    elif choice == 2: 

         # we are adding to an existing catalog, so we use the append
        catalog = open("product_catalog.txt", "a")

        # the lines below asks a user to enter product data, and assigns the input to variables
        product_code = input("Enter product code: ")
        product_price = input("Enter product price: ")
        product_description = input("Enter product description: ")

        # the lines below write the variables "product_code", "product_price", and "product_description" to the file
        # at the end of each line, we add a + "\n" <-- that creates a line break, just like pressing the enter key.
        catalog.write(product_code + "\n")
        catalog.write(product_price + "\n")
        catalog.write(product_description + "\n")

        # the line below closes the file:
        catalog.close()

    # choice 3 is to print a list of products
    elif choice == 3: 

        # below we open the file for reading
        catalog = open("product_catalog.txt","r")

        # here we read the file into a variable named catalog
        catalog = catalog.read()

        # the line below calls the splitlines() method. splitlines() changes a string into a list. Each line break is a new element in the list.  
        catalog = catalog.splitlines()

        # we know we a product has 3 parts, a code, price and description. 
        # we know each product is on a new line.
        # so we divde the length of our list by 3, and then we know how many product we have!
        number_of_products = len(catalog)

        for i in range(number_of_products):
            print(catalog[i])          

    # choice 4 is to quit
    elif choice == 4: 
        break

References[edit]