lecture 06 – reading and writing text files. at the end of this lecture, students should be able...

15
Lecture 06 – Reading and Writing Text Files COMPSCI 107 Computer Science Fundamentals

Upload: lawrence-heath

Post on 31-Dec-2015

230 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example

Lecture 06 – Reading and Writing Text Files

COMPSCI 107Computer Science Fundamentals

Page 2: Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example

2COMPSCI 107 - Computer Science Fundamentals

At the end of this lecture, students should be able to: Read text files Write text files

Example code: Word count of a text file Sum the numbers in a text file

Learning outcomes

Page 3: Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example

3COMPSCI 107 - Computer Science Fundamentals

Random numbers are pseudo-random Use a seed value

import random

random.seed(x)

random.choice(<sequence>)

Working with Random numbers

Page 4: Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example

4COMPSCI 107 - Computer Science Fundamentals

Get a reference to the file on disk open(filename, mode)

Modes ‘r’ = read ‘w’ = write ‘r+’ = read and write Default is text mode, append ‘b’ for binary

Reading a file

f = open(filename)f = open(filename, 'r')f = open(filename, 'wb')

Page 5: Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example

5COMPSCI 107 - Computer Science Fundamentals

Different ways to read the contents of the file When the end of file is reached, empty string is returned

Reading a text file

for line in f: print(line) #do something with the line

f.readline() # returns a single line (string) from the file

f.read() # returns the entire file as a string

f.readlines() # returns a list of lines (strings) from the file

Page 6: Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example

6COMPSCI 107 - Computer Science Fundamentals

Withautomatically closes the filehandles some of the exceptions

Avoiding memory issues for large files

with open('filename') as f: data = f.readlines()

with open('filename', 'w') as f: f.write('Hello World')

with open('filename') as f: for line in f: print(line)

Page 7: Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example

7COMPSCI 107 - Computer Science Fundamentals

Reading data from a filedef read_data(filename): '''Reads data from a file

Given the name of a file, reads all the data from a file and return the data as a list where each element in the list is a single piece of datum from the file. test2.txt contains the text: This is a small test file with a few words.

>>> read_data('test2.txt') ['This', 'is', 'a', 'small', 'test', 'file', 'with', 'a', 'few', 'words.'] ''' data = [] with open(filename) as f: for line in f: data += line.split() return data

Page 8: Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example

8COMPSCI 107 - Computer Science Fundamentals

Write a function that returns the word count for a text file.

Write a function that returns the number of unique words in a text file (where words are defined as a sequence of characters separated by white space).

Exercise

Page 9: Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example

9COMPSCI 107 - Computer Science Fundamentals

Read data as text Convert text to number

Functions that change types int() float() str()

Reading numbers from file

for line in f: number = int(line) #one value per line

Page 10: Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example

10COMPSCI 107 - Computer Science Fundamentals

Write a function that reads in a file of numbers and returns the sum of those numbers.

Exercise

Page 11: Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example

11COMPSCI 107 - Computer Science Fundamentals

Write a function called unique_words that accepts the name of a text file as an argument and produces a list of all the words in that file, without repeating any of the words.

Exercise

Page 12: Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example

12COMPSCI 107 - Computer Science Fundamentals

Write a function called difference that accepts two file names as arguments. The function should print out any line in the first file that is not identical to the same line in the second file

Exercise

Page 13: Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example

13COMPSCI 107 - Computer Science Fundamentals

Similar to reading data Write all the data Close the file after all the data is written.

Writing data to a file

f.write( string_to_write )f.close()

Page 14: Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example

14COMPSCI 107 - Computer Science Fundamentals

Write a function called word_list that accepts the name of a text file as an argument and produces a list of all the words in that file, sorted in order with one word per line.

Exercise

Page 15: Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example

15COMPSCI 107 - Computer Science Fundamentals

Files need to be opened before you can read or write to them f = open( filename[, mode] )

Files should be closed when you finish with them. f.close()

Reading a file f.read() f.readline()

Writing a file f.write( string )

Summary