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
Any help will be very appreciated.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With