Last active
September 7, 2015 21:51
-
-
Save ramalho/75d596068fbb017a806e to your computer and use it in GitHub Desktop.
Simple file handling exception example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| while True: | |
| filename = input('Enter the name of a file to open: ') | |
| try: | |
| # open for reading in binary mode, so any file will do | |
| fin = open(filename, 'rb') | |
| except OSError as exc: | |
| print('Something untoward happened [%r]' % exc) | |
| else: | |
| break | |
| try: | |
| chunk = fin.read(10) | |
| finally: | |
| fin.close() | |
| print('The first 10 bytes of the file are:') | |
| print(chunk) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment