File I/O, Exception, Assertion
COL 100 - Introduction to Computer Science
Department of Computer Science and Engineering
Indian Institute of Technology Delhi
Reading and Writing Files
It is a common task to
• read some input data file
• do some calculation/filtering/processing with the data
• write some output data file with results
Python distinguishes between
• text files ('t')
• binary files 'b')
If we don’t specify the file type, Python assumes we
mean text files.
Reading and Writing Files
Writing a text file
[Link]
Reading and Writing Files
Writing a text file
[Link]
Reading and Writing Files
Reading a text file
[Link]
Reading and Writing Files
Reading a text file
[Link]
Reading and Writing Files
Exercise
[Link]
Reading and Writing Files
Exercise
[Link]
Source:[Link]
programming-in-python-fall-2016/lecture-slides-code/
Source:[Link]
programming-in-python-fall-2016/lecture-slides-code/
Source:[Link]
programming-in-python-fall-2016/lecture-slides-code/
Source:[Link]
programming-in-python-fall-2016/lecture-slides-code/
Source:[Link]
programming-in-python-fall-2016/lecture-slides-code/
Source:[Link]
programming-in-python-fall-2016/lecture-slides-code/
Source:[Link]
programming-in-python-fall-2016/lecture-slides-code/
Exceptions
Exceptions
Assertion
• Want to be sure that assumptions on state of
computation are as expected
• use an assert statement to raise an exception
• an example of good defensive programming
Assertion
Source:[Link]
programming-in-python-fall-2016/lecture-slides-code/
Assertion
Source:[Link]
programming-in-python-fall-2016/lecture-slides-code/