From 08ffcbce8d23b442dc0f3e3bc8fd6f9e98b999d9 2021-12-09 18:37:41 From: Matthias Bussonnier Date: 2021-12-09 18:37:41 Subject: [PATCH] Merge pull request #13385 from nicolaslazo/master Expand ~ in filename arguments for %notebook, %save and %history magics --- diff --git a/IPython/core/magics/basic.py b/IPython/core/magics/basic.py index b44b4a3..f70605e 100644 --- a/IPython/core/magics/basic.py +++ b/IPython/core/magics/basic.py @@ -4,6 +4,7 @@ import argparse from logging import error import io +import os from pprint import pformat import sys from warnings import warn @@ -572,6 +573,7 @@ Currently the magic system has the following functions:""", For example, to export the history to "foo.ipynb" do "%notebook foo.ipynb". """ args = magic_arguments.parse_argstring(self.notebook, s) + outfname = os.path.expanduser(args.filename) from nbformat import write, v4 @@ -585,7 +587,7 @@ Currently the magic system has the following functions:""", source=source )) nb = v4.new_notebook(cells=cells) - with io.open(args.filename, 'w', encoding='utf-8') as f: + with io.open(outfname, "w", encoding="utf-8") as f: write(nb, f, version=4) @magics_class diff --git a/IPython/core/magics/code.py b/IPython/core/magics/code.py index 1451bd5..3f8100e 100644 --- a/IPython/core/magics/code.py +++ b/IPython/core/magics/code.py @@ -222,6 +222,7 @@ class CodeMagics(Magics): fname, codefrom = args[0], " ".join(args[1:]) if not fname.endswith(('.py','.ipy')): fname += ext + fname = os.path.expanduser(fname) file_exists = os.path.isfile(fname) if file_exists and not force and not append: try: diff --git a/IPython/core/magics/history.py b/IPython/core/magics/history.py index ad58b33..d49259f 100644 --- a/IPython/core/magics/history.py +++ b/IPython/core/magics/history.py @@ -151,6 +151,7 @@ class HistoryMagics(Magics): # We don't want to close stdout at the end! close_at_end = False else: + outfname = os.path.expanduser(outfname) if os.path.exists(outfname): try: ans = io.ask_yes_no("File %r exists. Overwrite?" % outfname)