File I/O, Exception, Assertion COL 100 - Introduction to Computer - - PowerPoint PPT Presentation
File I/O, Exception, Assertion COL 100 - Introduction to Computer - - PowerPoint PPT Presentation
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
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
https://www.southampton.ac.uk/~fangohr/training/python/pdfs/Python-for-Computational-Science-and-Engineering.pdf
Reading and Writing Files
Writing a text file
https://www.southampton.ac.uk/~fangohr/training/python/pdfs/Python-for-Computational-Science-and-Engineering.pdf
Reading and Writing Files
Reading a text file
https://www.southampton.ac.uk/~fangohr/training/python/pdfs/Python-for-Computational-Science-and-Engineering.pdf
Reading and Writing Files
Reading a text file
https://www.southampton.ac.uk/~fangohr/training/python/pdfs/Python-for-Computational-Science-and-Engineering.pdf
Reading and Writing Files
Exercise
https://www.southampton.ac.uk/~fangohr/training/python/pdfs/Python-for-Computational-Science-and-Engineering.pdf
Reading and Writing Files
Exercise
https://www.southampton.ac.uk/~fangohr/training/python/pdfs/Python-for-Computational-Science-and-Engineering.pdf
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- 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:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/
Assertion
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and- programming-in-python-fall-2016/lecture-slides-code/