If an existing file is opened in append mode what happens to the files existing contents

In-class worksheet 18¶

Apr 2, 2020

Reading and writing files in Python¶

We interact with files in python by opening the file and assigning it to a variable. This variable is called a file handle. The file handle is an object, and it has member fuctions such as .read() and .write() that allow us to either read the file contents or write to the file. After we are done working with the file, we must close the file handle.

We open a file and generate the associated handle with the open() function. This function takes two arguments:

  1. the name of the file to open
  2. the mode in which the file should be read. Modes, with usage examples, include,

    • read-only ("r"). Use this mode to read a file, but not write anything to it.

      • Usage: handle = open("filename.txt", "r")
    • write-only ("w"). Use this mode to write to an empty file.

      • Usage: handle = open("filename.txt", "w")
    • append ("a"). Use this mode to add content to an existing file.

      • Usage: handle = open("filename.txt", "a")

Opening files for reading¶

We will first work with a simple test file, called "testfile.txt". The file has the following contents:

This is line one of the file.
This is line two of the file.
This is line three of the file.

(You can download this file from the class website here: http://wilkelab.org/classes/SDS348/data_sets/testfile.txt)

We will open this file and read it in one go:

Note that the .read() method saves the entire body of the file to a single string. Another convenient way to read a file is to retrieve it as a list of lines, so that we can easily loop over the file contents. We can do this with the .readlines() method:

We see that .readlines() gives us a list with three entries, where each entry is one line in the file. We can also see that all entries of this list end with the symbol \n. This symbols represents the new-line character. It determines when one line ends and the next one begins.

Now that we have the file lines in a list, we can easily loop over them, and perform some calculations as needed:

You may notice that there is an empty line between each line of output. Can you guess why? See below in Problem 4 for an answer.

Opening files for writing¶

Opening a file in write-mode will overwrite the file, but opening in append-mode will add to the bottom of an existing file. Note that if we open a file for writing (or appending) that doesn't already exist, then a new file will be created with the specified name. By contrast, if we attempt to open a non-existing file for reading, we will receive an error message.

To write to a file, we use the .write() method on the file handle:

Note that the above code created a new file and wrote a single sentence to it. No matter how many times you execute this code, the file will have the same contents.

To add new contents to an existing file, open the file in append ("a") mode:

The with statement¶

As you can see from the above code examples, when we are dealing with files we need to write many blocks of code of the form .open(), code to interact with file, .close(). This can become cumbersome, and in particular we may forget to close some of the files that we opened. Not closing files can cause all sorts of trouble. For example, other applications may not be able to interact with a file until your program has properly closed it. Or, if you write a loop that opens many files but never closes them, you may crash your computer. Thus, since closing files is so critical, wouldn't it be nice if Python did this for us automatically? It will do so, if we're using the with statement.

In the with statment, instead of writing

file_handle = open(filename, mode)

we write

with open(filename, mode) as file_handle:
    ... # code block that operates on the file handle

The file will be closed automatically once we leave the block.

Thus, we could rewrite the last two examples with with in the following form:

Problem 1:

Download the file "road_not_taken.txt" from the class website: http://wilkelab.org/classes/SDS348/data_sets/road_not_taken.txt
(You may have to right-click the link and choose "save as". Make sure to save the file in the same location where your Jupyter notebook is.) This file contains the famous poem "The Road not Taken" by Robert Frost.

(a) Write a program that reads the file in one go and prints out the file contents.

(b) Write a program that reads in the file line by line and counts the total number of lines.

(c) Write a program that counts the number of letters in the file. Use the function count_letters() we have discussed in a previous class. Then print out how offen the different vowels (a, e, i, o, u) are used in this document.

Problem 2:

Read in the file "road_not_taken.txt", loop over every line in the file, identify the lines that contain the string "road" (ignoring case), and write those lines into a new file called "extracted_lines.txt". Then, read the file "extracted_lines.txt" back in and print its contents, to verify that everything worked right.

Problem 3:

Take the solution to one of your previous problems and rewrite them using a with statement. (Skip this problem if you have used with statements throughout.)

If this was easy¶

Problem 4:

Using one with statement and one for loop, open the file "road_not_taken.txt" as input file, convert it to upper case, and write it to a new file called "road_not_taken_upper.txt". Then read the newly generated file back in and print out the first 10 lines (+ line numbers), to make sure everything looks right. Make sure you don't get any empty lines between the individual lines you print.

Problem 5:

Instead of reading the file "road_not_taken.txt" from your own computer, read it directly from the web. Hint: Use the function urlopen() from the urllib package. And pay attention to the type of data that you receive from the handle created by urlopen().

When a file that already exists is opened in append mode the files existing contents are erased?

When a file that already exists is opened in append mode, the file's existing contents are erased. If you do not handle an exception, it is ignored by the Python interpreter, and the program continues to execute. You can have more than one except clause in a try/except statement.

When a file is opened in this mode data will be written at the end of the files existing contents?

If an existing file is opened in append mode, what happens to the file's existing contents? It will not be erased and new data will be written at the end of the file's current contents.

What happens if you try to open a file for input read mode but that file does not exist?

When you open a file for reading, if the file does not exist, the program will open an empty file.

When you open a file that file already exists on the disk using the W mode?

Terms in this set (9) When working with a sequential access file, you can jump directly to any piece of data in the file without reading the data that comes before it. When you open a file that file already exists on the disk using the "w" mode, the contents of the existing file will be erased.