File I/O, Exception, Assertion COL 100 - Introduction to Computer - - PowerPoint PPT Presentation

file i o exception assertion
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

File I/O, Exception, Assertion

COL 100 - Introduction to Computer Science

Department of Computer Science and Engineering Indian Institute of Technology Delhi

slide-2
SLIDE 2

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.

slide-3
SLIDE 3

Reading and Writing Files

Writing a text file

https://www.southampton.ac.uk/~fangohr/training/python/pdfs/Python-for-Computational-Science-and-Engineering.pdf

slide-4
SLIDE 4

Reading and Writing Files

Writing a text file

https://www.southampton.ac.uk/~fangohr/training/python/pdfs/Python-for-Computational-Science-and-Engineering.pdf

slide-5
SLIDE 5

Reading and Writing Files

Reading a text file

https://www.southampton.ac.uk/~fangohr/training/python/pdfs/Python-for-Computational-Science-and-Engineering.pdf

slide-6
SLIDE 6

Reading and Writing Files

Reading a text file

https://www.southampton.ac.uk/~fangohr/training/python/pdfs/Python-for-Computational-Science-and-Engineering.pdf

slide-7
SLIDE 7

Reading and Writing Files

Exercise

https://www.southampton.ac.uk/~fangohr/training/python/pdfs/Python-for-Computational-Science-and-Engineering.pdf

slide-8
SLIDE 8

Reading and Writing Files

Exercise

https://www.southampton.ac.uk/~fangohr/training/python/pdfs/Python-for-Computational-Science-and-Engineering.pdf

slide-9
SLIDE 9

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/

slide-10
SLIDE 10

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/

slide-11
SLIDE 11

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/

slide-12
SLIDE 12

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/

slide-13
SLIDE 13

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/

slide-14
SLIDE 14

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/

slide-15
SLIDE 15

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/

slide-16
SLIDE 16

Exceptions

slide-17
SLIDE 17

Exceptions

slide-18
SLIDE 18

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
slide-19
SLIDE 19

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/

slide-20
SLIDE 20

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/