[Tutor] Opening a cmd.exe

Alan Gauld alan.gauld at btinternet.com
Sun Mar 22 17:58:52 CET 2009

"Mark Tolonen" <metolone+gmane at gmail.com> wrote

> Does this do what you want?  It creates a new cmd window titled 
> "Dir", then executes some commands.

> import os
> os.system('start cmd /c title Dir ^&^& dir ^&^& pause')

It didn't do quite what I expected, but it made me look at the
help for start, which says:

Starts a separate window to run a specified program or command.

START ["title"] [/Dpath] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
      [/WAIT] [/B] [command/program]

    "title"     Title to display in  window title bar.
    path        Starting directory

So it looks like simply running start and specifying a title string
before the command should do the trick:

>>> os.system('start "my window" cmd.exe')

Note the title MUST be in double quotes so the string to system must
either be rw or use single quotes.

You learn somethhing new...

Alan G
Author of the Learn to Program web site

If you want the window to stay open after executing
> the commands, use /k instead of /c.  'cmd /?' gives other switches 
> you might want.  The escaping(^) of the ampersands(&) is required or 
> the commands will run in the current console not the new one.
> import os
> os.system('start cmd /c title Dir ^&^& dir ^&^& pause')
> -Mark
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list