How to prevent "Open With" dialog window when opening file from command window?

I associated Python's interpreter with Python scripts using environment variable:

C:\Users\Piotr>ftype Python.File
Python.File="%PYTHON_HOME%\python.exe" "%1" %*

Sometimes this environment variable is unset or set to non-existing path. When it's the case and I try to run Python script like this

C:\Users\Piotr>some_script.py

a dialog window titled Open With pops up.

I'd rather get some error info in the command line window instead of this dialog. How can I prevent this dialog from popping up in this situation?


Solution 1:

My over-convoluted answer:

Permanently set the environment variable:

%PYTHON_HOME%=C:\Python\NotReady

Then in that folder create a small program named python.exe that displays an error message, or better yet, starts the python initialisation script.

Solution 2:

You could try testing the result of executing ASSOC .py before actually invoking the script:

ASSOC .py >nul && some_script.py

The ASSOC .py command displays the file type associated with the .py extension, if any. If there's no association (which must be true when there's no Python installed in the system), the command sets the ERRORLEVEL system variable.

Now, the command before the && command delimiter is only executed if the previous command hasn't set ERRORLEVEL. So, if there's no file type association for .py in the system, the script will not be invoked. Also you'll get a relevant message from ASSOC in the command window. (>nul suppresses 'normal' output, but doesn't suppress the No association message.)