3

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 :

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.

Michael Baudin
  • 1,022
  • 10
  • 25

4 Answers4

6

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.

James
  • 32,991
  • 4
  • 47
  • 70
  • Thank you very much: I create a Jupyter Notebook and was able to reproduce this. However, the second time I execute the Notebook, the following message appears: File 'session.txt' exists. Overwrite? y Overwriting file. and this is annoying. The "-f" flag does not mean "force overwrite" here. – Michael Baudin Nov 27 '19 at 21:32
  • Correct. The `-f` is short for `file`. If the file exists it asks before overwritting. – James Nov 28 '19 at 08:20
5

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.

Jason K Lai
  • 1,500
  • 5
  • 15
2

For a pure Python solution, you can use code.interact to invoke the interactive Python console. Redirect the standard output to a io.StringIO object to capture the output into a variable:

import code
import sys
from io import StringIO

def readfunc(prompt):
    try:
        line = next(file).rstrip()
    except StopIteration:
        raise EOFError
    print(prompt, line, sep='')
    return line

# or with open('script.py') as file:
file = StringIO('''for i in range(3):
    print(i)

x=1.
x''')
stdout = sys.stdout
sys.stdout = StringIO()
code.interact(readfunc=readfunc)
output = sys.stdout.getvalue()
sys.stdout = stdout
print(output)

This outputs:

Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> for i in range(3):
...     print(i)
... 
0
1
2
>>> x=1.
>>> x
1.0
blhsing
  • 91,368
  • 6
  • 71
  • 106
0

The common and trivial solution is to use the facilities of the shell to run Python.

prompt$ python script.py arguments "more arguments" >log.txt 2>&1

The redirection >log.txt says to write standard output to this file, and 2>&1 says to write standard error to the same place.

This is not suitable for interactive use (running Python with a script argument will disable the >>> prompt and the other system messages you get when running Python interactively); the script answer is better if you really require that functionality.

tripleee
  • 175,061
  • 34
  • 275
  • 318