5.5. Sequential Files#

A sequential file is a type of file where data is stored in sequence, i.e. one record after another. This organisation is simple and commonly used for storing and processing data that will be accessed in order, or in which the access order isn’t important.

5.5.1. Key Features#

  • Ordered Storage: Data is written and stored in a specific sequence.

  • Linear access: Data is accessed sequentially, i.e., starting from the beginning and moving through each record in turn.

For example, if the list library books in the previous challenge were saved to a file, we could use a sequential file. Sequential files are often used for things like logs and data backups.

5.5.2. Opening and Closing Files#

We open and close files in Python using open and close.

variable = open(filepath, mode)
variable.close()

It’s really important you remember to close your file at the end otherwise there is a risk that your might accidentally corrupt your file and your data won’t be saved properly!

The mode you use when opening a file will depend on what you want to do with the file. We use:

  • 'r' to read from the file

  • 'a' to append to the end of the file

  • 'w' which writes to the file by overwriting any contents the file previously had

5.5.3. Writing To A File#

We can write to a file using write, which will write a single line to a file. Note that we use \n at the end of each line to insert a newline character. For example:

f = open("filename.txt", "a")

f.write("New entry.\n")
f.write("Second entry.\n")

f.close()

Here we open the file using 'a' so that we don’t overwrite existing data.

5.5.4. Reading From A File#

We can read from a file a number of ways.

  • readline allows you to read in one line at a time and gives you each line as a string

  • readlines allows you to read in the remaining lines into a list of strings

  • read reads the entire file into a string.

Consider the following files notes.txt.

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.

We can read from this line by line and print each line as we go. Let’s just read the first two lines.

f = open("/course/notes.txt", "r")
line1 = f.readline()
print(line1)
line2 = f.readline()
print(line2)
f.close()

Note that there is an extra newline! This is because line 1 contains 'Beautiful is better than ugly.\n', i.e. it has a newline character built in. This means when it gets printed we end up with extra lines. This is the same with the second line.

print("Beautiful is better than ugly.\n")
print("Explicit is better than implicit.\n")

If we want to read multiple lines in at once we can do so with readlines. This will read all of the remaining lines in the file into a list.

f = open("/course/notes.txt", "r")
lines = f.readlines()
f.close()
print(lines)

You can access the lines from this list in the usual way you work with lists. For example, we can loop through all of the lines.

f = open("/course/notes.txt", "r")
lines = f.readlines()
f.close()

for i in lines:
    print(i)

The final option we have is to use read, which reads in the entire file as a single string.

f = open("/course/notes.txt", "r")
text = f.read()
f.close()

print(text)

5.5.5. With Open#

Often it can be easy to forget to close your file. So an alternative is to use:

with open(filepath, mode) as variable:
    # code that executes while the file is open

Note that the code you run while the file is open is indented. At the end of the indented code block the file will automatically close.

with open("/course/notes.txt", "r") as f:
    line1 = f.readline()
    remaining = f.readlines()

print(line1)
print(remaining)

Note that readlines only reads the remaining lines.

5.5.6. File Errors#

If you try to open a file that does not exist you will get a FileNotFoundError. In this example the file missing.txt doesn’t exist.

f = open("missing.txt", "r")
f.close()

If you open the file using the wrong mode, e.g. if you try to read a file in write mode you will get an OSError.

with open("/course/notes.txt", "w") as f:
    line = f.readline()

5.5.7. Pseudocode#

Reading from files often appears in pseudocode. Keywords that are often associated with file usage in pseudocode are:

  • Open, to open a file

  • Read, to read the contents of a file (this will be line by line for sequential files)

  • Write, to write information to a file

  • Close, to close the file at the end

You’ll also see EOF, which means End of File, which can be used to tell you when you’re at the end of the file. Here is an example of some pseudocode that reads in lines from the file notes.txt, stores each line in the variable line and then displays each line.

Open notes.txt for input
Read line from notes.txt
WHILE not at EOF
    Display line
    Read line from notes.txt
ENDWHILE
Close notes.txt
Question 1

Which of the following opens secret_stuff.txt in Python so that you can read it? Select all that apply.

  1. with open('secret_stuff.txt', 'a') as f:
    
  2. with open('filename.txt', 'w') as f:
    
  3. with open('filename.txt', 'a') as f:
    
  4. with open('secret_stuff.txt', 'r') as f:
    
  5. f = open('secret_stuff.txt', 'r')
    
  6. f = open('secret_stuff.txt', 'w')
    
Solution

To read from the file secret_stuff.txt you need:

  • The correct filename secret_stuff.txt

  • To specify read mode using ‘r’

Both

with open('secret_stuff.txt', 'r') as f:

and

f = open('secret_stuff.txt', 'r')

will work, but if you use the second option don’t forget to close the file with f.close() when you are done!

Question 2

What does the following code do?

with open('file.txt', 'w') as f:
    f.write('ABC\n')
  1. Reads ABC\n from file

  2. Writes ABC\n to file

  3. It will results in a FileNotFoundError

  4. It will result in an OSError

Solution

Solution is locked