0

This question is specific to using the python -i command (not IPython): how can I limit the exported history to commands from the current session only?

Specifically, when I run Python via terminal (python -i) and then type the following commands:

from pathlib import Path
x = Path('abc')
from readlines import write_history_file
write_history_file('history.txt')

The exported file contains code from previous Python sessions also, while I am interested only in the code from the current session (in the snippet above this would be the first two commands).

SultanOrazbayev
  • 14,900
  • 3
  • 16
  • 46

1 Answers1

0

Using [Doskey][1]/history we can all previous command. ONLY WORKS ON WINDOWS. You can use readlines if you are using IPYTHON

import subprocess

cmd_history = subprocess.check_output(["doskey", "/history"])
cmd_history_list = [line.strip() for line in cmd_history.decode("utf-8").splitlines()]
python_cmd_history = [line for line in cmd_history_list if line.startswith("python")]
print(python_cmd_history)
Flow
  • 846
  • 9
  • 24