Previous Lecture Lecture 18 Next Lecture

Lecture 18, Thu 03/07

Recursion cont., File IO

Recorded Lecture: 3_7_24

Exploring All Possibilities with Recursion

scramble.jpeg

Files

File Basics

File I/O:

  1. Open the file (creates a connection between your program and the file)
    • Choose if the connection will be for reading, writing, or appending to a file
  2. Read the data / write the data
  3. Close the file (close the connection). This needs to be done once per opened file
    • Note that your code will probably run OK if you forget to close the file for small programs not dealing with a lot of file IO
    • However, your computer uses resources to keep track of all open files. Forgetting to close a file will be problematic if your program is performing a lot of file IO during runtime

Common Ways to Read Data From Files

This is Line 1
This is Line 2
This is Line 3
# Reading entire contents of file into program
infile = open('example.txt') 
data = infile.read()
print(data)
infile.close()