Sunday, May 24, 2009

Avoid the *.py extension when calling scripts under Windows

If you'd like to make your scripts run by typing

myscript args

instead of

myscript.py args

you can wrap the python script inside a *.bat file. Say you want to run myscript.py and it takes two arguments, make the following batch DOS file:

myscript.py %1 %2

The percentage+number things play the role of sys.argv in Python such that command line arguemnts are piped through to the Python script.

2 comments:

  1. Add .py to your PATHEXT environment variable and you can run Python files at the command line without the extension. No need to wrap in a batch file.

    ReplyDelete
  2. Ah... good point. (I come from the other camp... just playing with Windows now and then. Thanks!)

    ReplyDelete