lecture17

Lecture17 - Lecture 16 Odds and Ends part 2 Caltech CS 1 Fall 2010 Class inheritance Raising exceptions using raise Creating your own exception

Info iconThis preview shows pages 1–15. Sign up to view the full content.

View Full Document Right Arrow Icon
Caltech CS 1: Fall 2010 Lecture 16 : November 22, 2010 Odds and Ends, part 2
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 Class inheritance Raising exceptions using raise Creating your own exception classes The pass keyword
Background image of page 2
Caltech CS 1: Fall 2010 A variety of topics that don't fit neatly into any category: Writing to files print >> sys.stderr The type() function The None type and value Default arguments to functions Functions with arbitrary numbers of arguments Defining functions inside functions break and continue
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 A variety of topics that don't fit neatly into any category: method chaining list slice assignment list stride notation the // operator short-circuiting behavior of and and or
Background image of page 4
Caltech CS 1: Fall 2010 We've seen how to read from files using the readline() method on files e.g. myfile = open('temps.txt', 'r') while True: line = myfile. readline () if not line: break # do something with line. .. myfile.close()
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 We've also seen how to read from files using the for statement, e.g. myfile = open('temps.txt', 'r') for line in myfile: # do something with line myfile.close()
Background image of page 6
Caltech CS 1: Fall 2010 Writing to files can also be done using a method: the write() method on file objects Before doing this, the file to be written to has to be opened for writing or appending # writing myfile = open('results.txt', 'w' ) # appending myfile = open('results.txt', 'a' )
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 Opening a file using the 'w' argument to the open() function creates a new file removing any previous file with that name! Opening a file using the 'a' argument to the open() function creates a new file only if no such file already exists If it already exists, writing to the file just appends new lines to the end of the existing file
Background image of page 8
Caltech CS 1: Fall 2010 Writing to a file is done using the write() method on file objects myfile = open('results.txt', 'w') myfile. write ('this is line 1\n') myfile. write ('this is line 2\n') myfile. write ('this is line 3\n') myfile.close()
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 This will result in a new file called results.txt being created in the current directory with the lines: this is line 1 this is line 2 this is line 3
Background image of page 10
Caltech CS 1: Fall 2010 Note that you must put newlines in explicitly when you use the write() method: myfile = open('results.txt', 'w') myfile.write('this is line 1 \n ') myfile.write('this is line 2 \n ') myfile.write('this is line 3 \n ') myfile.close()
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 If you forget to do this e.g. myfile = open('results.txt', 'w') myfile.write('this is line 1') myfile.write('this is line 2') myfile.write('this is line 3') myfile.close()
Background image of page 12
Caltech CS 1: Fall 2010 This will result in: this is line 1this is line 2this is line 3 (common beginner's mistake)
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
There is another way to write to files using the print statement: myfile = open('results.txt', 'w')
Background image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/22/2011 for the course CS 1 taught by Professor Pinkston,d during the Fall '08 term at Caltech.

Page1 / 57

Lecture17 - Lecture 16 Odds and Ends part 2 Caltech CS 1 Fall 2010 Class inheritance Raising exceptions using raise Creating your own exception

This preview shows document pages 1 - 15. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online