A slight improvement to Jacob's answer:
@echo off
pushd c:\py
c:\python271\python.exe %*
popd
Save this as py.cmd in one of the directories from your your PATH environment variable. Then you can call
py sample.py arg1 arg2 ...
This works with any number of arguments.
But, as wberry mentioned, you could change the working directory from inside your Python script as well, if you really need to (but I think that's a bad idea):
os.chdir(os.path.abspath(os.path.dirname(__file__))) #untested
While this isn't exactly an answer to your question, I recommend using the following pattern:
Say, I have a Python script c:\mydir\myprog.py that requires special environment variables (PATH, ORACLE_HOME, whatever) and maybe it needs a particular working directory.
Then I create a file myprog.cmd in the same directory:
@echo off
setlocal
set PATH=...
set ORACLE_HOME=...
pushd %~dp0
python %~dpn0.py %*
popd
endlocal
The pushd/popd part is for changing and restoring the working directory.
For an explanation of the %~... Syntax, type
help call
at the command prompt.
This approach gives you full control about the environment of your Python program.
Note that the python call is generic: If you need a second Python script otherprog.py, then just save a copy of myprog.cmd as otherprog.cmd.