Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

Highlights from this chapter include:

  • Python can read and write data in files using the built-in open() function.
  • Due to buffering, changes to a file may not be visible until the file is closed.
  • Newline characters need to be removed/added when reading/writing files.
  • Comma-separate-value (CSV) files are commonly used to represent data.
  • Exceptions that cause a program to terminate can be handled using try and except.
  • Raising an exception is an alternative way to return from a function when an error occurs.
Statement Description

file = open("myfile.txt")

Open a file for reading.

file = open("myfile.txt", 'w')

Open a file for writing.

file = open("myfile.txt", 'a')

Open a file for appending.

file.close()

Close a file after making changes.

data = file.read()

Read the entire contents of a file. The variable data is a string.

data = file.readline()

Read the next line of a file. The variable data is a string.

data = file.readlines()

Read all lines of a file. The variable data is a list of strings.

file.write("Have a nice day!\n")

Writes a line to a file. In contrast to print(), the write() function does not automatically append a newline.

file.write(["Line 1\n", "Line 2\n"])

Writes multiple lines to a file. As with write(), a newline is not automatically added at the end of each line.

try:
    # Statements
except:
    # Statements
Try to run statements that might raise an error. If any error is raised, run other statements.
try:
    # Statements
except ValueError:
    # Statements

Try to run statements that might raise a ValueError. If a ValueError is raised, run other statements.

try:
    # Statements
except ValueError as err:
    # Statements

Try to run statements that might raise a ValueError. If a ValueError is raised, run other statements. The variable err contains more details about the ValueError.

raise ValueError

Raises a ValueError with no specific error message.

raise ValueError("number is prime")

Raises a ValueError with the message "number is prime".

Table 14.3 Chapter 14 reference.
Citation/Attribution

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 permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a 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 digital format, then you must 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

© Jul 30, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo 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.