3

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
  • 33,190
  • 5
  • 92
  • 111
Jonathan Porter
  • 1,365
  • 7
  • 34
  • 62
  • 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
  • 24,943
  • 3
  • 48
  • 66
  • 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
  • 14,531
  • 8
  • 95
  • 135
  • My understanding is that `readline` is not for Windows. I believe that the question is in a Windows context. – user2153235 Jun 25 '23 at 23:28
  • @user2153235 No, there is [pyreadline3](https://github.com/pyreadline3/pyreadline3) for python on windows. However, there were a number of legacy [issues and PR's](https://github.com/pyreadline3/pyreadline3/issues/5) that were never fixed/merged in that, so I'm using the fork from [here](https://github.com/eabase/pyreadline3), which (AFAIK), also was not merged/fixed in pyreadline3. – not2qubit Jun 26 '23 at 00:24
  • I'm just getting to know Python and Conda, so I don't feel savvy enough to deviate from `conda install pyreadline3`. So far, the file from `pyreadline.BaseReadline.write_history_file` has a size of 0 bytes (details [here](https://stackoverflow.com/questions/76566358/write-history-filepyhistory-str-object-has-no-attribute-mode#comment134997894_76566474)). – user2153235 Jun 27 '23 at 16:30
  • Much troubleshooting later: After importing `pyreadline3` and assigning `readline = pyreadline3.BaseReadline()`, I found that `readline.get_current_history_length()` returns 0 while `readline.get_history_length()` returns 100. From `help(readline)`, this means that no commands are being stored in the history, but there is room to store 100 commands. – user2153235 Jun 28 '23 at 04:52
  • I've posted my trials and tribulations as their own question [here](https://stackoverflow.com/questions/76570300/conda-on-windows-10-python-commands-not-stored-in-command-history) – user2153235 Jun 28 '23 at 16:53
  • @user2153235 `Conda` was created back in the days when Python package management was very unstable and with sketchy platform/compile support. I strongly suggest to move away from *Conda* for any future and regular development. There are also better package management tools now compatible with `pip` such as `poetry` etc. – not2qubit Jun 28 '23 at 16:56
  • Point taken, but I don't feel that option is realistic for me at the moment. I am a newbie and need to align with how things are done not only in my team and organization, but also with the much more senior person that I am hoping to support, once I become somewhat functionally capable (which should have been last year). Having said that, I'm unsure of how central Conda is to the question of a command history file. Granted, most IDEs will probably have a way of providing the user with a history, e.g., Spyder, as distributed in Anaconda, so maybe the root issue is using Python without an IDE. – user2153235 Jun 28 '23 at 17:27