diff --git a/IPython/core/magics/code.py b/IPython/core/magics/code.py index 7f569a3..d37843b 100644 --- a/IPython/core/magics/code.py +++ b/IPython/core/magics/code.py @@ -402,9 +402,8 @@ class CodeMagics(Magics): self.shell.hooks.editor(filename) # and make a new macro object, to replace the old one - mfile = open(filename) - mvalue = mfile.read() - mfile.close() + with open(filename) as mfile: + mvalue = mfile.read() self.shell.user_ns[mname] = Macro(mvalue) @skip_doctest