Setting win32 console title from Python

jay graves jaywgraves at gmail.com
Thu Apr 28 09:59:40 EDT 2005


Hmm.

>From an interactive interpreter this works for me.

import os
os.system('title Jay')

but the title returns to its previous value when I Ctrl-Z out of the
process.

If I save this as a file and run it, it seems to work without spawning
a new window but resets it the title after the program finishes like
above.

import os
os.system('title Jay')
x = raw_input()

You mention that the SetConsoleTitle api resets itself after the script
finishes so I'm assuming that 'title' command is just calling the same
api under the covers.

What is your requirement specifically?  I do something similar but in a
different way but it might not be what you are after.

I have a 'projects' directory where I keep all of my work.  I have
written a small python script 'p.py' that I call like this

p [s|e|*] projectname

the 's' is for shell
the 'e' is for explorer window
the '*' is for both shell and explorer

If there is only one argument, I assume it is the project name and I
default the other argument to 'e'.

if the projectname doesn't have any wildcard characters, I append a '*'
and glob my project directory with that value.  if the glob call only
returns a single value, I go ahead and do what was requested (open a
shell or explorer window to that directory)  if there is more than one
value returned, I present a numbered menu of project directories that
match and wait for input on which one to open.

The point to all this, is that when I open a shell from p.py I use this
command.
os.system(r'start "%s" /D%s\%s' % (proj,directory,proj))

This spawns a new cmd window with the title of my project name.
Spawning the process with the correct name from the beginning seems to
do the trick.
But like I said, I don't really know your exact requirements.

HTH.
...
jay




More information about the Python-list mailing list