Skip to content

Instantly share code, notes, and snippets.

@ramalho
Last active September 7, 2015 21:51
Show Gist options
  • Select an option

  • Save ramalho/75d596068fbb017a806e to your computer and use it in GitHub Desktop.

Select an option

Save ramalho/75d596068fbb017a806e to your computer and use it in GitHub Desktop.
Simple file handling exception example
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