31

I'm running several instances of a certain Python script on a Windows machine, each from a different directory and using a separate shell windows. Unfortunately Windows gives each of these shell windows the same name:

<User>: C:\Windows\system32\cmd.exe - <script.py>

Is it possible to set this name to something else through a Python command?

user3666197
  • 1
  • 6
  • 50
  • 92
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359

10 Answers10

54

On Windows, a simple console command will suffice:

from os import system
system("title " + myCoolTitle)

Nice and easy.

Nick is tired
  • 6,860
  • 20
  • 39
  • 51
ShouravBR
  • 685
  • 7
  • 10
  • 1
    I moved the correct answer mark to this answer as it works and has no dependencies – Jonathan Livni Apr 20 '12 at 11:34
  • 5
    @Jonathan Jeffrey Harper's answer is better as there are also no dependencies and in addition it doesn't invoke shell. – Piotr Dobrogost May 15 '13 at 21:38
  • 2
    The benefit of this answer is the cmd shell's builtin `title` command prepends `Administrator: ` to the title if the user has admin rights. – Eryk Sun Jan 18 '17 at 20:33
  • Another advantage with this is that it can be preceded with `system("cls")` to clear the window from old clutter. – dotswe Nov 09 '21 at 03:08
48

This works for Python2.7 under Windows.

>>> import ctypes
>>> ctypes.windll.kernel32.SetConsoleTitleA("My New Title")
Jeffrey Harper
  • 748
  • 7
  • 7
  • 28
    Or ctypes.windll.kernel32.SetConsoleTitleW("My New Title") for python 3 – VGE Oct 16 '14 at 10:26
  • How about the reverse? Open an instance of python from command prompt and specify the title of the new python instance. – Musixauce3000 May 04 '16 at 11:51
  • 3
    For the most portability and independence from cached prototypes on `ctypes.windll` (the worst idea in ctypes), use `kernel32 = ctypes.WinDLL('kernel32', use_last_error=True);` `kernel32.SetConsoleTitleW(u"My New Title")`. An `errcheck` function with signature `(result, func, args)` should also be set. It should `raise ctypes.WinError(ctypes.get_last_error())` if the result is false (i.e. the call failed), which enables idiomatic Python programming with exceptions instead of crude C error codes. – Eryk Sun Jan 18 '17 at 20:32
  • There another DIFFERENCE between ctypes and system. The latter will keep 'Command Prompt - "C:\Anaconda3\condabin\conda.bat" activate ' and APPEND the title, whereas ctypes will CLEAR it, so it's better if you have many (narrow?) windows. – user7660047 Apr 18 '22 at 22:12
25

Due to not enough rep I cannot add a comment to the above post - so as a new post.

In Python 3 you can use:

import ctypes
ctypes.windll.kernel32.SetConsoleTitleW("My New Title")

I edited this answer: please remark, that it now uses SetConsoleTitleW, which is the Unicode version of the SetConsoleTitle function. This way you can use unicode and no longer have to encode the string/variable to a byte object. You can just replace the argument with the string variable.

user136036
  • 11,228
  • 6
  • 46
  • 46
  • 1
    Writing UTF-8 to an ANSI API is nonsense. You'll get mojibake. You should always prefer the native wide-character APIs in Windows programming. The ANSI APIs are all but deprecated. – Eryk Sun Jan 18 '17 at 20:35
  • Thank you for the comment. You are correct, I edited the answer. – user136036 Jan 19 '17 at 00:33
6

Since you're only going to be running this on Windows (IOW, there's not a cross-platform way to do this):

  1. Download & install the Win32 extensions for python
  2. Inside of your script, you can change the title of the console with the function

    win32console.SetConsoleTitle("My Awesome App")

bgporter
  • 35,114
  • 8
  • 59
  • 65
5

Comparison of the posted system() & windll-based methods

tying to add a small quantitative comparison of latency overheads associated with two of the posted methods:

|>>> from zmq import Stopwatch
|>>> aSWX = Stopwatch()

|>>> from os import system
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  15149L [us]
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  15347L [us]
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  15000L [us]
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  14674L [us]
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  14774L [us]
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  14551L [us]
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  14633L [us]
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  15202L [us]
|>>> aSWX.start();system( 'TITLE os_SHELL_CMD_TITLE_TXT');aSWX.stop()  14889L [us]

|>>> from ctypes import windll
|>>> aSWX.start();windll.kernel32.SetConsoleTitleA('DLL');aSWX.stop()   5767L [us]
|>>> aSWX.start();windll.kernel32.SetConsoleTitleA('DLL');aSWX.stop()    643L [us]
|>>> aSWX.start();windll.kernel32.SetConsoleTitleA('DLL');aSWX.stop()    573L [us]
|>>> aSWX.start();windll.kernel32.SetConsoleTitleA('DLL');aSWX.stop()    749L [us]
|>>> aSWX.start();windll.kernel32.SetConsoleTitleA('DLL');aSWX.stop()    689L [us]
|>>> aSWX.start();windll.kernel32.SetConsoleTitleA('DLL');aSWX.stop()    651L [us]

In cases, where one might spend about a half of millisecond ( but not some tens of that ) the windll.kernel32 method seems promising and may serve better for an alternative display of a WatchDOG / StateVARs / ProgressLOG / auto-self-diagnostic messages, being efficiently displayed in a soft real-time need, during long running processes.

user3666197
  • 1
  • 6
  • 50
  • 92
3

Use:

import ctypes
ctypes.windll.kernel32.SetConsoleTitleW('new title')

Or:

import os
os.system('title new title')
Pixelsuft
  • 97
  • 5
3

I am not aware of a way to change the cmd window title from within the script.

However, you can set the title when launching the script if you use the start command.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 1
    This is a nice way to do it as it has no dependencies, however in my case the script only knows the intended window name after it reads a configuration file... – Jonathan Livni Sep 13 '11 at 05:19
2

If starting the Idle-shell is an option instead of the cmd shell:

idle.py [-c command] [-d] [-e] [-s] [-t title] [arg] ...

-c command  run this command
-d          enable debugger
-e          edit mode; arguments are files to be edited
-s          run $IDLESTARTUP or $PYTHONSTARTUP first
-t title    set title of shell window
Remi
  • 20,619
  • 8
  • 57
  • 41
1

It is now possible to change the window title from within any language via outputting a standard escape sequence to the console (stdout). Here's a working example from a batch file Change command prompt to only show current directory name however just printing ESC close-bracket 2 semicolon your-title-here BEL (control-G) will do it. Also an easily adapted PHP example:

function windowTitle($title)
  {printf("\033]2;%s\007", $title);}
mike_n
  • 11
  • 2
  • 2
    This is not an answer in python code (which is a requirement of the question). Please ensure your answers have the correct scope to the question. – LightCC Aug 04 '22 at 20:31
  • @mike_n Irregardless of the comment above, this was useful to me, thank you. – Vopel Sep 21 '22 at 17:46
1

Using OS module To interact with the terminal

  1. Import the library - import os
  2. Interact with the terminal to change title - os.system('title your_tile')

Explanation:

So if you open your terminal and type title your_title is will change the terminal title to your_title CMD

In Python the OS.system() is used to type into the terminal

This is how you will change title of terminal with Python

unofficialdxnny
  • 105
  • 1
  • 7