0% found this document useful (0 votes)
6 views20 pages

Python Lecture 18-File Input Output

The document covers file input/output operations in Python, highlighting the distinction between text and binary files. It also discusses the use of assertions for defensive programming to ensure that assumptions about the state of computation are correct. Additionally, it includes links to external resources for further reading and exercises.

Uploaded by

Abhishek Goutam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views20 pages

Python Lecture 18-File Input Output

The document covers file input/output operations in Python, highlighting the distinction between text and binary files. It also discusses the use of assertions for defensive programming to ensure that assumptions about the state of computation are correct. Additionally, it includes links to external resources for further reading and exercises.

Uploaded by

Abhishek Goutam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

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

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/

You might also like