Repository: ipython/ipython

Issue:
%%writefile can't handle white space in file path/filename
It is as the title suggests. In ipython, I have tried

`%%writefile  "test out.txt"`
`%%writefile  'test out.txt'`
`%%writefile  test out.txt`
`%%writefile  test\ out.txt`

and none gives me the expected behavior. The first one leads to creating a file literally named "test out.txt"  (yes, with quotes). The third one just leads to 

>  unrecognized arguments: out.txt

Is this a bug or am I just missing something obvious? Using IPython 6.5.0 with Python 3.6.5.
%%writefile can't handle white space in file path/filename
It is as the title suggests. In ipython, I have tried

`%%writefile  "test out.txt"`
`%%writefile  'test out.txt'`
`%%writefile  test out.txt`
`%%writefile  test\ out.txt`

and none gives me the expected behavior. The first one leads to creating a file literally named "test out.txt"  (yes, with quotes). The third one just leads to 

>  unrecognized arguments: out.txt

Is this a bug or am I just missing something obvious? Using IPython 6.5.0 with Python 3.6.5.


Discussion:
This is probably a bug, it shouldn't be too hard to fix, it will just take some time to figure out exactly how to parse the line. 

Here is where the [code of writefile is](https://github.com/ipython/ipython/blob/032cc8c92986762204a801eed36c57822b4a6345/IPython/core/magics/osm.py#L763-L791).

THe parsing is based on argparse.
This is probably a bug, it shouldn't be too hard to fix, it will just take some time to figure out exactly how to parse the line. 

Here is where the [code of writefile is](https://github.com/ipython/ipython/blob/032cc8c92986762204a801eed36c57822b4a6345/IPython/core/magics/osm.py#L763-L791).

THe parsing is based on argparse.

Resolving patch:
diff --git a/IPython/core/magics/osm.py b/IPython/core/magics/osm.py
--- a/IPython/core/magics/osm.py
+++ b/IPython/core/magics/osm.py
@@ -777,8 +777,11 @@ def writefile(self, line, cell):
         The file will be overwritten unless the -a (--append) flag is specified.
         """
         args = magic_arguments.parse_argstring(self.writefile, line)
-        filename = os.path.expanduser(args.filename)
-
+        if re.match(r'[\'*\']|["*"]', args.filename):
+            filename = os.path.expanduser(args.filename[1:-1])
+        else:
+            filename = os.path.expanduser(args.filename)
+            
         if os.path.exists(filename):
             if args.append:
                 print("Appending to %s" % filename)


Test patch:
diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py
--- a/IPython/core/tests/test_magic.py
+++ b/IPython/core/tests/test_magic.py
@@ -27,7 +27,8 @@
 from IPython.testing import decorators as dec
 from IPython.testing import tools as tt
 from IPython.utils.io import capture_output
-from IPython.utils.tempdir import TemporaryDirectory
+from IPython.utils.tempdir import (TemporaryDirectory,
+                                    TemporaryWorkingDirectory)
 from IPython.utils.process import find_cmd
 
 
@@ -797,7 +798,20 @@ def test_file_amend():
             s = f.read()
         nt.assert_in('line1\n', s)
         nt.assert_in('line3\n', s)
-        
+
+def test_file_spaces():
+    """%%file with spaces in filename"""
+    ip = get_ipython()
+    with TemporaryWorkingDirectory() as td:
+        fname = "file name"
+        ip.run_cell_magic("file", '"%s"'%fname, u'\n'.join([
+            'line1',
+            'line2',
+        ]))
+        with open(fname) as f:
+            s = f.read()
+        nt.assert_in('line1\n', s)
+        nt.assert_in('line2', s)
     
 def test_script_config():
     ip = get_ipython()