Link Search Menu Expand Document

File Handling

Table of contents

  1. Description
  2. File Streams
  3. The open() Function
  4. Instructional Videos

Description

Variables store data during the execution of the program, but “disapear” when the program ends. Data saved in files will persist beyond the execution of the program. Python provides methods for programmers to read, write, and modify file contents.

File Streams

In order to read or write file data, you must always do three things in order:

  1. Open the file.
  2. Perform some processing,
  3. Close the file.

Additionaly, your file stream can only go in one direction. This means that you cannot write to a file you open for reading, and vice versa. You specify how you are going to use the file when you open it by indicating the file mode:

  • r” mode: Opens the file for reading only.
  • w” mode: Opens the file for writing only.
  • a” mode: Allows the programmer to append additional data to the end of a file.

The open() Function

Instructional Videos