Reputation: 1151
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.
Upvotes: 3
Views: 4793
Reputation: 189367
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.
Upvotes: 0
Reputation: 106513
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
Upvotes: 2
Reputation: 36608
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.
Upvotes: 6
Reputation: 1540
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.
Upvotes: 5