Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Windows command-line terminal title in Python

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?

like image 398
Jonathan Livni Avatar asked Sep 12 '11 11:09

Jonathan Livni


People also ask

How can I change the title of the CMD window?

From a command prompt or from the Run command, type -title text to specify the title for the window.

Can you use cmd in Python?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!


4 Answers

This works for Python2.7 under Windows.

>>> import ctypes
>>> ctypes.windll.kernel32.SetConsoleTitleA("My New Title")
like image 61
Jeffrey Harper Avatar answered Oct 17 '22 23:10

Jeffrey Harper


On Windows, a simple console command will suffice:

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

Nice and easy.

like image 31
ShouravBR Avatar answered Oct 17 '22 23:10

ShouravBR


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.

like image 24
user136036 Avatar answered Oct 17 '22 23:10

user136036


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")

like image 6
bgporter Avatar answered Oct 17 '22 23:10

bgporter