Basic Hints for Windows Command Line Programming
Python, Windows & the Command Line
Python, Windows & the Command Line
Most new programmers who use Windows are unfamiliar with using the command line, but it can be a powerful tool. It is especially useful for writing small scripts that can accomplish complex tasks. A command line interface can even be easier to use than a crowded graphical interface. The command line is known variously as the console, the CLI (Command Line Interface) or the Shell environment.
One motivation for using the console is that it is a lot easier to write simple tools that use the command line than it is to wrestle with graphical toolkits.
The real power of the command line is that it allows you to use several different tools from within the same environment.
There are a couple of things you can do in order to make the most of the command line from Windows. This tutorial will assume that you are running Windows XP. It is aimed at the Python programmer, but the first section is general information that maybe helpful to anyone wanting to use the Windows command line shell.
It does not cover Powershell , which is the new fangled windows shell environment. Powershell is effectively a programming environment, and Python is a much better environment. There are interesting possibilities in using the Powershell libraries from IronPython, but that is a subject for another day.
To launch the command line console (the black console box thingy), perform the following steps :
- Open the Start menu
- Click the Run... option
- Type cmd /d into the text box and hit enter
This will launch the command console. The current directory will be shown before the prompt, it will look something like :
You can now run command line programs by typing their name. So long as the command is on the path (explained in a minute) or in the current directory, it will run and possibly print out some results to the console.
If you mistype the command, or Windows can't find it, you will get the response :
'fish' is not recognized as an internal or external command,operable program or batch file.
The 'official' documentation for the command shell begins with the Command Shell Overview. As we go I'll point you to more useful online resources.
We launched the console by typing cmd /d. The /d part is an instruction (a command line argument) to the cmd command which launches the console. Here is a reference to the command interpreter (cmd) which includes a list of all the possible options.
The Windows shell environment is definitely not as powerful as the Unix type shells (this is what prompted Microsoft to create Powershell). You can use a Unix shell environment by installing Cygwin or MinGW. There are however quite a few 'standard' commands available to the Windows. You can see a full list of them at the Command Line Reference A-Z
The two most useful commands are cd and dir.
cd means change directory, and changes the current directory you are in. Unfortunately windows has an odd habit (well, rule really) of having a current directory per device.
So, if your current directory is C:\WINDOWS, just typing cd D:\Somewhere doesn't change the current directory. To do that type d: (windows is not case sensitive for device, directory or filenames). This will then switch you to the current directory on your 'd' device.
dir means 'directory', and it lists all the files in the current directory. See the dir reference page for a list of the different command line options you can pass to the dir command.
Next we'll look at some things you can do to setup your command line environment for general use.
Having to go through the Start -> Run -> cmd dance everytime you want to use the command line can put you off.
Thankfully, setting up a desktop shortcut is very easy.
- Right click on (above?) the desktop and select the New -> Shortcut option.
- This should create a blank shortcut and launch the 'Shortcut Wizard'.
- Where it says 'Type the location of the item:', type cmd /d followed by enter.
- On the next screen you can leave the default name cmd.exe (or change it if you want to), this should create you a shiny new shortcut.
Having created the shortcut there are a few things to configure. There is a Microsoft guide to Configuring the Command Prompt but we'll cover the most important points here.
Right click on your shortcut and select Properties. This will bring up the dialog shown above.
Even if you are not running cmd from a shortcut, you can still configure some of these options.
Whilst the console is running, right click on the console icon in the task bar.
When you have modified the properties, after hitting 'Ok' make sure that you select the radio button Save properties for future windows with same title.
Select the Shortcut tab at the top of the properties dialog. The interesting option is the Start in: one. This will be the current directory when you start the console. The default is %WINDIR%, which is usually C:\WINDOWS.
Next select the Options tab and make sure that the QuickEdit mode, Insert mode and Discard Old Duplicates check boxes are all checked.
QuickEdit is particularly important, it means you can drag and select areas of the screen. The selected area will show in white. Hit enter, or right click, to put the selection onto the clipboard. To enter the contents of the clipboard at the cursor point just right click. (By the way, when you have a selection on your console it can freeze running programs.)
Select the Layout tab and in the Screen buffer size set the Height: to 999. This will store the maximum number of lines of the console output.
Environment variables are system (or user) wide settings that programs and Windows can use to control behaviour. Particularly relevant to working with the command line are the PATH and PATHEXT environment variables.
The PATH environment variable contains a list of paths that Windows searches when you enter a command. If you don't specify an explicit path to a command (a full path looks something like C:\Python25\python.exe), windows first looks in the current directory and then it checks all the directories specified in the path. Only if the command can't be found in any of these directories does the console return the 'something' is not recognized as an internal or external command... message.
The most important thing to check is that Python is on your path. I think that the Windows installer for Python puts it on the path, but I have worked on systems where this is not the case.
Open the command console and type python. If Python is on your path you will see the Python interpreter start :
If it isn't you will get the message 'python' is not recognized as an internal or external command....
To add a directory to the path go to the start menu and select the Control Panel. If you are using classic menus you might find this under the Settings menu.
From the control panel double click on the System icon. When the system dialog comes up select the Advanced tab :
From here you can edit environment variables by clicking on the Environment Variables button.
To put Python on the path we will edit the user environment variable PATH. (Windows environment variables are not case sensitive.) The user environment variables are the one in the top list box. If the path environment variable already exists. select it and hit Edit. If it doesn't already exist, hit new and enter PATH in the Variable name: text box.
The contents of this variable needs to be a list of paths, separated by semi-colons. To make sure that the user variable doesn't overwrite the system variable it needs to start with %PATH%. This will keep existing system paths in the user variable and add any extra ones.
Assuming you don't already have an existing path user variable and your Python installation is in C:\Python25, your new path variable should be :
It can be extremely convenient to add extra directories to the path, that way you can store your own commands and programs in these directories and use them from anywhere. This is particularly useful in conjunction with the PATHEXT environment variable.
I'm sure that you, oh astute reader, will have noticed that the command to launch the Python interpreter, once it is on the path, is python; but the file that is launched is python.exe.
Windows knows what to do with executable files, or at least ones that have the .exe file extension, it runs them. The same is true for .com and .bat files. If they are on the path you can run them without specifying the file extension.
It is also possible to get Windows to automatically launch Python programs without having to specify the Python interpreter or the full path.
The PATHEXT environment variable specifies which file types can be launched in this way. By default is looks something like .COM;.EXE;.BAT;.CMD. To add Python files to the list follow the instructions from the PATH variable to edit it. Assuming you don't have a user PATHEXT variable, create it and set it to the following :
Windows knows how to launch Python files through the file associations set-up by installing Python.
Once you've added Python files to PATHEXT, you can use Python scripts without the added burden of having to type python before them all the time.
You can also put your Python scripts into a directory and add this to the path. If you then have a Python script like r2w.py you can use it by just typing r2w at the console.
Windows uses file associations to know how to launch programs.
Sometimes these file associations get lost, or you may want to create new file associations. For example you could create Python scripts with the extension .ipy  and associate them with IronPython.
If the file-type already has an association you will need to right click on the file and select the Open with option.
If there is no file association for this file type you can just select Open. This will bring up the annoying Windows cannot open this file dialog :
Choose Select the program from a list and move on.
This will bring up the Open With dialog :
You'll have to browse and choose the executable you want to associate with the file type. Make sure that the Always use the selected program... check box is checked.
If this is a new file extension, and you want to be able to use these files at the command line, don't forget to add the extension to the PATHEXT environment variable.
|||Previously known as Monad.|
|||IPython users please don't shout at me.|
For buying techie books, science fiction, computer hardware or the latest gadgets: visit The Voidspace Amazon Store.
Last edited Tue Aug 2 00:51:34 2011.