Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a Python session, including input and output, as a text?

Tags:

python

I have a Python script:

x=1.
x

and I would like to generate the following text from the command line:

Python 3.7.4 (default, Aug 13 2019, 20:35:49) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x=1.
>>> x
1.0

and save it into a file.

This is the content that I can get by interactively copying and pasting the code into the Python interpreter.

I considered the following messages :

  • How to save a Python interactive session? but this shows how to save the Python statements, not the output in the console.
  • How can I save all the variables in the current python session? but this shows how to save the variables, not the content of the session.
  • how to save python session input and output is exactly what I want, but has no answer.

I considered

  • using IPython and the "%save" magic, but this only saves the Python statements, not the output of the statements,
  • using Jupyter Notebook, but the exports are in PDF, tex, HTML, etc... while I only want plain text.

Any help will be very appreciated.

like image 530
Michael Baudin Avatar asked Sep 05 '25 03:09

Michael Baudin


2 Answers

If you are using IPython, you can export your history, including inputs and outputs, to a file using the %history magic command.

%history -o -p -f session.txt

The -o flag add outputs, the -p flag shows the classic Python prompt, and the -f flag exports to a file.

The session.txt file for my latest session looks like this:

>>> import pandas as pd
>>> df = pd.read_clipboard()
>>> df
    time  a  b
0  0.000  6  5
1  0.008  6  9
2  0.016  1  9
3  0.024  2  7
4  0.032  1  5
>>> x =  [-6, -4, -3, -2, -1, 0.5, 1, 2, 4, 6]
>>> df['a_'] = df.a.apply(lambda r: x[r-1])
>>> %history?
>>> %history -o -p -f session.txt

It is worth noting that only outputs are shown. The text from print statements does not appear.

like image 123
James Avatar answered Sep 07 '25 22:09

James


Assuming that this is being run via a standard GNU/Linux terminal environment, one can consider the script command to make a typescript of the terminal session. This is useful for all sorts of applications, not specifically for recording python sessions. Basically, the order of how one would use it in this situation is the following:

$ script
$ python
>>> python commands
>>> exit()
CTRL-D
cat typescript

The output will be created in a typescript file in the working directory. It is not a purely text file, but it is pretty close if you are just hoping to record the python portion.

like image 28
Jason K Lai Avatar answered Sep 07 '25 22:09

Jason K Lai