{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture17 - Lecture 16 Odds and Ends part 2 Caltech CS 1...

Info icon This preview shows pages 1–16. 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
Image of page 1

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

View Full Document Right Arrow Icon
Caltech CS 1: Fall 2010 Class inheritance Raising exceptions using raise Creating your own exception classes The pass keyword
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
Image of page 3

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

View Full Document Right 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
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()
Image of page 5

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

View Full Document Right 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()
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' )
Image of page 7

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

View Full Document Right 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
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()
Image of page 9

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

View Full Document Right 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
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()
Image of page 11

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

View Full Document Right 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()
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)
Image of page 13

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

View Full Document Right Arrow Icon
Caltech CS 1: Fall 2010 There is another way to write to files using the print statement: myfile = open('results.txt', 'w') print >> myfile, 'this is line 1' print >> myfile, 'this is line 2' print >> myfile, 'this is line 2' myfile.close()
Image of page 14
Caltech CS 1: Fall 2010 There is another way to write to files using the print statement: print >> myfile, 'this is line 1'
Image of page 15

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

View Full Document Right Arrow Icon
Image of page 16
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern