Skip to content

Instantly share code, notes, and snippets.

@92hackers
Created August 18, 2017 12:55
Show Gist options
  • Save 92hackers/d87bd46a116762325e5706e07bc56281 to your computer and use it in GitHub Desktop.
Save 92hackers/d87bd46a116762325e5706e07bc56281 to your computer and use it in GitHub Desktop.
Wrap open in a context manager, you must implement __enter__ and __exit__ methods
class File(object):
def __init__(self, file_name, method):
self.file_obj = open(file_name, method)
def __enter__(self):
return self.file_obj
def __exit__(self, type, value, traceback):
self.file_obj.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment