
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Catch EOFError Exception in Python
An EOFError is raised when a built-in function like input() or raw_input() do not read any data before encountering the end of their input stream. The file methods like read() return an empty string at the end of the file.
The given code is rewritten as follows to catch the EOFError and find its type.
Example
#eofError.py try: while True: data = raw_input('prompt:') print 'READ:', data except EOFError as e: print e Then if we run the script at the terminal $ echo hello | python eofError.py
Output
prompt:READ: hello prompt:EOF when reading a line
Advertisements