7

When I run any Python script, I would like to see the script's filename appear in the Windows command line window's titlebar. For example, if I run a script called "mytest.py", I want to see "mytest" in the titlebar. I would like this to be automatic, so I don't have to add code to every one of my scripts.

Currently I'm attempting to do this with sitecustomize.py, because when Python is run, including from double-clicking a Python script, sitecustomize is imported before the script runs.

I've tried getting __main__'s __file__ and sys.argv, but sitecustomize doesn't see either:

file sitecustomize.py:

import __main__, sys
print "hasattr __main__.__file__:", hasattr(__main__, "__file__")
print "hasattr sys.argv:", hasattr(sys, "argv")
print "-" * 60

file mytest.py:

import sys
print "__file__ is:", __file__
print "sys.argv is:", sys.argv
raw_input() # don't end the script immediately

output:

hasattr __main__.__file__: False
hasattr sys.argv: False
------------------------------------------------------------
__file__ is: C:\Documents and Settings\Owner\Desktop\mytest.py
sys.argv is: ['C:\\Documents and Settings\\Owner\\Desktop\\mytest.py']
4
  • 1
    I am not sure what your question is? Why can't you just use __file__ ? Commented Jun 26, 2011 at 18:11
  • I want to do it for every script, without adding it to every one of my python scripts manually. sitecustomize.py is imported any time Python starts up. Is something else unclear? Commented Jun 26, 2011 at 18:20
  • Your question does not make sense, hence no answers. Please show us how you are planing on using this. Commented Jun 27, 2011 at 17:12
  • I tried rephrasing my question. I hope it's easier to understand. I'm just planning on using it for my own convenience - say if I have many scripts open, I want to identify them at a glance. Commented Jun 27, 2011 at 21:08

2 Answers 2

5

I'm glad you asked! I now have it working for my scripts, and it's pretty cool.

Here's the code:

import sys
import time
from ctypes import windll

class SetTitle(object):
    def __del__(self):
        time.sleep(1)
        command = ' '.join(sys.argv)
        windll.kernel32.SetConsoleTitleA(command)

sys.argv = SetTitle()

This is for Python 2.x -- for 3.x you need to change SetConsoleTitleA to SetConsoleTitleW (last letter changes from A to W).

How it works: since the sys.argv object does yet exist, I create an object and assign it to sys.argv; then, when Python assigns the actual argv to sys.argv, my object is tossed, and the __del__ method is called; the __del__ method is then able to access the real argv and set the title bar accordingly. I put the 1 second sleep in just to avoid any possible race conditions, but I'm not sure it's necessary. If you don't want to see all the command-line args, you can pre-process command any way you like.

My thanks to the folks on python-win32 mailing list, and Thomas Heller in particular, for helping with the 'set title' portion of this question.

Sign up to request clarification or add additional context in comments.

2 Comments

Wouldn't it be cleaner to make sys.argv a descriptor?
@PiotrDobrogost: descriptors only work with class attributes, and argv is a module attribute. So, if it could work, it would be cleaner -- but it won't.
1

When I run any Python script, I would like to see the script's filename appear in the Windows command line window's titlebar. For example, if I run a script called "mytest.py", I want to see "mytest" in the titlebar. I would like this to be automatic, so I don't have to add code to every one of my scripts.

I think you should add this functionality to all your scripts by a module and not by hacking it into sitecustomize.py. Also even if you still want to go the sitecustomize path you will need to pass __file__ from your script, which means you will not get around to add some code to all your scripts.

What you certainly can do is to put that code into a module and then import it in all your python scripts. Like I mentioned above, you need to pass __file__ from your main script otherwise you will get the modules filename. Also there is no need to import __main__ to retrieve __file__.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.