Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to write the value of a variable in a %%writefile magic command in IPython notebook?

I want to be able to use a variable to output some text in IPython Notebook. I can write to a file from within IPython Notebook using the %%writefile command. I can also use a variable to name the file using $varaible_name.

file_name = "file.out"
some_variable = "text"

New cell

%%writefile $file_name
????some_varaible  #<-what command goes here

So that the output file (named "file.out") looks like this:

text

like image 587
burkesquires Avatar asked Oct 15 '14 14:10

burkesquires


People also ask

What does %% Writefile do in Python?

%%writefile lets you output code developed in a Notebook to a Python module. The sys library connects a Python program to the system it is running on. The list sys. argv contains the command-line arguments that a program was run with.

What does %% do in Jupyter notebook?

Both ! and % allow you to run shell commands from a Jupyter notebook. % is provided by the IPython kernel and allows you to run "magic commands", many of which include well-known shell commands. ! , provided by Jupyter, allows shell commands to be run within cells.


1 Answers

Now this is possible using the following command::

%store varname > filename.txt

More about %store

like image 73
AndreyT Avatar answered Sep 21 '22 08:09

AndreyT