I would like to be able to do the following during an IPython session:
- Easily save the contents of a variable to disk in an ASCII file
- Easily re-direct stdout to disk for a given command
by easily I mean for example by using a magic (i.e. I would like to avoid having to type multi-line statements in IPython, opening file descriptors, etc.).
This answer in this thread suggests using
%edit some_variable
to open the value of a variable in the editor (based on the value of $EDITOR) from which I could later save things to disk. This sounds great but when I try it on a regular Python variable
> my_variable = 'Hello world'
> %edit a
I get TypeError: 'NoneType'object is not iterable (By the way, I know that my $EDITOR env. variable works well, since other programs I use rely on it. either way $EDITOR is emacsclient in my case)
I have also tried with:
%save 'test.txt' print(my_variable)
with the hope that it would re-direct the output of the statement print(my_variable) to test.txt, but instead I get the following error:
'print(a)' was not found in history, as a file, url, nor in the user namespace.
Any thoughts on how to accomplish this?