1 of 15

CSE 160

File IO

Hannah Cheung

2 of 15

Announcements

  • HW1 grades released, resubmissions due Sunday, October 22
  • Programming Activity + Written Activity 3 released, due Friday, October 20
  • HW3 released, due Friday, October 27

2

3 of 15

Files

  • Files store information when a program is not running
  • Important operations:
    • open a file
    • close a file
    • read data
    • write data

3

4 of 15

File objects and filenames

  • A file object represents data on your disk drive
    • Is an object in your Python program that you create
    • Can read from it and write to it in your program
  • A filename (usually a string) states where to find the data on your disk drive
    • Can be used to find/create a file
    • Linux/Mac uses forward slashes (/)
      • Ex: “/home/hfc/class/160/lectures/file_io.pptx”
    • Windows uses backward slashes (\)
      • Ex: “C:\Users\hfc\class\160\lectures\file_io.pptx”
    • Ex: “Husky.png”

4

5 of 15

Types of filenames

  • An absolute filename gives a specific location on disk:
    • Starts with “/” (Unix) or “C:\” (Windows)
      • Ex: “/home/hfc/class/160/lectures/file_io.pptx”, “C:\Users\hfc\class\160\lectures\file_io.pptx”
    • Warning: code will fail to find file if you move/rename files or run program on a different computer
  • A relative filename gives a location relative to the current working directory
    • Ex: “lectures/file_io.pptx”, “images\Husky.png”
    • Warning: code will fail to find the file unless you run your program from a directory that contains given contents

5

6 of 15

Filename Examples

  • Linux/Mac: This could all refer to the same file
    • “home/hfc/class/160/homework3/images/Husky.png”
    • “homework3/images/Husky.png”
    • “images/Husky.png”
    • “Husky.png”
  • Windows: These could all refer to the same file
    • “C:\Users\hfc\class\160\homework\images\Husky.png”
    • “homework3\images\Husky.png”
    • “images\Husky.png”
    • “Husky.png”

6

7 of 15

Current Working Directory

  • Current working directory in Python - directory from which you ran Python program
    • Code to determine current working directory:

import os

print(“Current working directory is”, os.getcwd())

7

8 of 15

Opening Files

# Takes a filename and returns a file object.

# Fails if file cannot be found and opened.

# By default, file is opened for reading.

my_file = open(“datafile.dat”)

my_file = open(“data.file.dat”, “r”)

# Opens file for writing.

# Creates given file if it does not exist. If file

# already exists, then it will be overwritten.

my_file = open(“datafile.dat”, “w”)

# Writing/appending to already-existing file.

my_file = open(“datafile.dat”, “a”)

8

9 of 15

Reading Files

# Takes a filename and returns a file object.

# Fails if file cannot be found and opened.

# By default, file is opened for reading.

my_file = open(“datafile.dat”)

# Approach: Process one line at a time

for line_of_text in my_file:

…process line_of_text

# Approach 2: Process entire file at once

all_data_as_big_string = my_file.read()

# close the file when done reading

my_file.close()

9

10 of 15

Reading File Example

# Reads in file one line at a time and

# prints contents of the file.

in_file = “student_info.txt”

my_file = open(in_file)

for line_of_text in my_file:

print(line_of_text)

my_file.close()

10

11 of 15

Reading File Example

# Count number of words in text file.

in_file = “student_info.txt”

my_file = open(in_file)

num_words = 0

for line_of_text in my_file:

word_list = line_of_text.split()

num_words += len(word_list)

my_file.close()

print(“Total words in file:”, num_words)

11

12 of 15

Reading a file multiple times

  • Iterating a file uses it up - cannot read it more than once.

12

my_file = open(“datafile.dat”)

for line_of_text in my_file:

…process line_of_text

# loop will not be executed

for line_of_text in my_file:

…process line_of_text

13 of 15

Reading a file multiple times

  • Iterating a file uses it up - cannot read it more than once.
    • Solution 1: Read file into list, then iterate over it.

13

my_file = open(“datafile.dat”)

lines = []

for line_of_text in my_file:

lines.append(line_of_text)

for line_of_text in lines:

…process line_of_text

for line_of_text in lines:

…process line_of_text

14 of 15

Reading a file multiple times

  • Iterating a file uses it up - cannot read it more than once.
    • Solution 1: Read file into list, then iterate over it.
    • Solution 2: Re-create file object (slower, but better if file does not fit in memory)
    • Try to avoid reading files more than once. Reading files is slow.

14

my_file = open(“datafile.dat”)

for line_of_text in my_file

…process line_of_text

my_file = open(“datafile.dat”)

for line_of_text in my_file:

…process line_of_text

15 of 15

Writing to Files

15

# Replaces any existing file of this name

my_file = open(“datafile.dat”, “w”)

# Similar to printing output, except must

# explicitly specify new lines using \n

my_file.write(“a bunch of data”)

my_file.write(“a line of text\n”)

# Argument provided must be a string

my_file.write(4) # Error!

my_file.write(str(4))

# close the file when done writing

my_file.close()