Setting win32 console title from Python

Duncan Booth duncan.booth at invalid.invalid
Thu Apr 28 08:20:07 EDT 2005


runes wrote:
> I'm trying to set the title of the console window (CMD.EXE) in Windows.
> I want it set to the basename of the current directory and it should
> stay after the script has finished.
> 
> Any ideas?
> 
I don't think you can do that.

Whenever you start an application from the command prompt the title is 
modified by appending a dash and the name of the program you started. When 
the application terminates the title is reset (to remove the name of the 
running program). So any change to the title will only last until the next 
time CMD.EXE prompts for input. The exception is that any title set using 
the TITLE command becomes the reset title for that command prompt.

I can think of only one way round this, which is to run your python program 
from a batch file and find some way to pass the desired title back to the 
batch file where you can set it with the TITLE command. For example, you 
could write the title to a temporary file, or if your program doesn't 
produce much other output print it to stdout and parse the program output 
using a for command:

e.g. This sets the title to the current time:
 
C:\>FOR /F "tokens=*" %i in (
More? 'python -c "from time import *; print asctime(localtime())"'
More? ) DO @TITLE %i

C:\>

Obvious notes: In a batch file you would double the % characters, and you 
don't type C:\> or More? as these are the prompts from CMD.EXE. Also this 
won't work on really old versions of CMD.EXE or with COMMAND.COM.



More information about the Python-list mailing list