In-class worksheet 18¶Apr 2, 2020 Show
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 We open a file and generate the associated handle with the
Opening files for reading¶We will first work with a simple test file, called
(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 We see
that 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 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 In the
we write
The file will be closed automatically once we leave the block. Thus, we could rewrite the last two examples with Problem 1: Download the file (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 Problem 2: Read in the file Problem 3: Take the solution to one of your previous problems and rewrite them using a If this was easy¶Problem 4: Using one Problem 5: Instead of reading the file 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.
|