[Python] how to enable seperate action for double click and drag and drop for python files

Discussão em 'Python' iniciado por Stack, Janeiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    I have a simple python script and I want to enable dragging and dropping files into the python script in Windows 11. This works. But only if I set python launcher as default application for python file types. I want to launch my editor when double clicking. My idea is to create a batch file as default application for the python script and launch my editor if no parameters are passed and launch it with python launcher if it has parameters. I cannot get it to fully work. Either the python script doesn't launch or window is created that doesn't close automatically.

    test drag.py:

    import sys

    if __name__ == "__main__":
    if len(sys.argv) > 1:
    with open("test.txt", 'w', newline='\n') as f:
    f.write("file names:")
    f.write("\n")

    for file_path in sys.argv[1:]:
    f.write(file_path)
    f.write("\n")

    else:
    with open("test.txt", 'w', newline='\n') as f:
    f.write("no file names")
    f.write("\n")


    python_drag.cmd:

    @echo off

    if not "%~2"=="" (
    ::arguments detected calling it with python
    START python.exe "%1" %*
    ) else (
    ::no arguments detected opening it with editor (vscode)
    START /B code %1
    )


    The key in Computer\HKEY_CLASSES_ROOT\Python.File\shell\open\command is set to "path_to\python_drag.cmd" "%L" %*

    This doesn't work as it doesn't launch the script.

    I've also tried: cmd /c call "‪path_to\python_drag.cmd" "%1" %*

    I don't know why it doesn't work. I tried many combinations of different commands.

    Continue reading...

Compartilhe esta Página