2

On Windows 10, Python 3.6

Let's say I have a command prompt session open (not Python command prompt or Python interactive session) and I've been setting up an environment with a lot of configurations or something of that nature. Is there any way for me to access the history of commands I used in that session with a python module for example?

Ideally, I would like to be able to export this history to a file so I can reuse it in the future.

Example:

Type in command prompt: python savecmd.py and it saves the history from that session.

Eryk Sun
  • 31,432
  • 3
  • 82
  • 100
Jonathan Porter
  • 1,802
  • 6
  • 29
  • 55
  • 2
    Windows or *nix? You put cmd, so i'm assuming Windows? – PressingOnAlways Jul 03 '17 at 23:31
  • @cᴏʟᴅsᴘᴇᴇᴅ I'm not referring to the interactive Python command prompt or Python interpreter – Jonathan Porter Jul 03 '17 at 23:34
  • @JonathanPorter If you're on *Nix, you can do `history = open('/home/.bash_history').readlines()` and you'd get everything from there. Thing is, I'm not sure how you'll limit it to the current session. – cs95 Jul 03 '17 at 23:41
  • Similar question for those who want to take editing history from Python further: https://stackoverflow.com/questions/53518255/print-bash-history-using-python – Josiah Yoder Jul 08 '20 at 19:49

2 Answers2

3

You don't need Python at all, use doskey facilities for that, i.e.:

doskey /history

will print out the current session's command history, you can then redirect that to a file if you want to save it:

doskey /history > saved_commands.txt

If you really want to do it from within Python, you can use subprocess.check_output() to capture the command history and then save it to a file:

import subprocess

cmd_history = subprocess.check_output(["doskey", "/history"])
with open("saved_commands.txt", "wb") as f:
    f.write(cmd_history)
zwer
  • 23,595
  • 3
  • 41
  • 57
  • 1
    You could do this in Python via ctypes without doskey.exe, and better even since you could use Unicode instead of the console codepage (which may be a lossy encoding of what you typed if it's not a character in the current codepage). However, it's not supported since Microsoft has never documented the console functions that doskey.exe calls for this: `GetConsoleCommandHistoryLengthW` and `GetConsoleCommandHistoryW`. – Eryk Sun Jul 03 '17 at 23:47
  • Seem that it's been removed from ctypes. I use: `for i in list(range(readline.get_current_history_length())): print(readline.get_history_item(i))`. – not2qubit Jan 17 '22 at 15:03
0

You're actually asking for 3 different things there.

  1. Getting Python REPL command hisotry
  2. Getting info from "prompt", which I assume is the Powershell or CMD.exe.
  3. Save the history list to a file.

To get the history from inside your REPL, use:

for i in list(range(readline.get_current_history_length())): print(readline.get_history_item(i))

Unfortunately, there is no history in REPL, when using from outside, as above, so you need to find the history file and print it.

To see the history file from powershell:

function see_my_py_history{ cat C:\<path-to>\saved_commands.txt }

Set-Alias -Name seehist -Value see_my_py_history
not2qubit
  • 11,992
  • 7
  • 83
  • 112