Skip to ContentGo to accessibility page

Chapter 14

14.1 Reading from files

1.
c. The file is opened and is associated with the fileobj object. Reading from the file will be possible using fileobj.
2.
a. The file must exist in the same folder as the Python file in order for the file to be opened.
3.
b. Python interprets this command as the programmer trying to print the object details. The object details are printed. Ex: <_io.TextIOWrapper name='newfile.txt' mode='r' encoding='cp1252'>.
4.
c. The read() function is called on fileobj. The file's contents will be read into file_str.
5.
b. The readlines() function is called on fileobj. file_lines is the string list ['Hello world!\n', 'How are you?\n'].
6.
a. The readline() function reads the next line of a file. Since readline() is called right after opening the file the first line is read. The resulting string "Hello world!" is printed. Note that readline() includes the '\n' character so the print will include an additional newline.

14.2 Writing to files

1.
a. The default mode for the open() function is read.
2.
c. 'a' indicates append mode allowing additions to the file.
3.
b. 'w' indicates write mode, and newlog.txt will be created or overwritten (if newlog.txt already exists) and will allow writing.
4.
c. resources.txt is likely empty, but the state of resources.txt is uncertain. To ensure the contents are saved correctly, the close() function must be used.
5.
a. Write mode will overwrite the existing file. The close() statement finalizes changes to the file.
6.
b. Append mode adds to the existing file. resources.txt is opened such that additions can be made to the file.

14.3 Files in different locations and working with CSV files

1.
b. The path followed by the filename enables the file to be opened correctly and in read mode by default.
2.
c. The use of forward slashes / replacing the Windows standard backslashes \ enables the path to be read correctly. The path followed by the filename enables the file to be opened and when reading a file read mode is preferable.
3.
c. The file is created or overwritten and changes can be written into the file.
4.
a. Since the backslashes in the Windows path appear in the string argument, which usually tells Python that this is part of an escape sequence, the backslashes must be ignored using an additional backslash \ character.
5.
a. The newline \n character indicates where line breaks are in a file and is used by the readlines() function to tell lines apart.
6.
b. The cell at the 2nd row and 3rd column contains '268' .
7.
c. The first line of the books.csv is read into csv_read. The first line is 'Title, Author, Pages' , which is the same as the first row.

14.4 Handling exceptions

1.
c. The string "3-4" is not an integer, so the int() function raises a ValueError like before.
2.
a. The filename should end with .txt, not .text.
3.
b. If the line has no spaces, then len(parts) will be only 1, and parts[1] will be out of range.
4.
b. The int() function raises a ValueError when trying to convert "one" to an integer, so the program jumps to the except clause.
5.
c. The program crashes and displays this error because the except clause does not handle ValueError.
6.
b. If an except clause does not specify a specific error type, then any error type is handled.

14.5 Raising exceptions

1.
a. "M" is a valid size, so the Pizza object is constructed.
2.
c. PizzaError is not a built-in exception type.
3.
a. An exception can be raised without providing an error message. In this case, the error message is empty, but the exception name is still printed.
Citation/Attribution
Reuse and redistribution of this content in digital or print format:
  • This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's prior written permission.
  • This book uses the Creative Commons Attribution-NonCommercial-ShareAlike License, which means that you can reuse and modify the material only for noncommercial purposes, must attribute OpenStax, and must distribute any derivative works under the same license.
  • Any commercial printing of this textbook, including using a local or custom printer, must be approved by OpenStax, and proper citation provided.
  • OpenStax-copyrighted images, activities, assessments, and similar components of this book are subject to the same licensing – CC-BY-NC-SA. They can be used for noncommercial purposes with attribution. Commercial use requires permission.
  • Permission requests: Anyone who intends to incorporate this content (including text, images, and other components) into large language models, use it in AI offerings, use it commercially (including in print), and/or has questions about another use case is welcome to complete our reuse request form.
Attribution information
  • If you are redistributing all or part of this book in a noncommercial print format, then you must include on every physical page the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

  • If you are redistributing all or part of this book in a noncommercial digital format, then for every page that includes OpenStax content, you must license the derivative work under the same CC-BY-NC-SA license as the original, and include on every digital page view the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

Citation information

The information below includes the information needed to generate citations in most major styles (APA, MLA, etc.); you must reformat and organize the information as needed to fit the requirements of the style. Use the information below to generate a citation. We recommend using a citation tool such as this one.

© Apr 23, 2026 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike License. The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo, and Rice University name, and Rice University logo trademarks, or wordmarks are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.