[Python] Escaping quotes in Python subprocesses for Windows

Discussão em 'Python' iniciado por Stack, Setembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to terminate a Python program that uses many threads on its own.

    If I'm not mistaken, just sys.exit() works fine.

    However, to guard against my many mistakes, including losing references to threads, I tried the following:

    subprocess.Popen(['start', 'cmd.exe', '/c', f'timeout 5&taskkill /f /fi "PID eq {os.getppid()}"'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)


    I thought it was a problem with escaping the quotes, so I tried several things but failed. I gave up and did the following and it worked perfectly.

    with open('exit_self.bat', 'w') as file:
    file.write(f'timeout 5&taskkill /f /fi "PID eq {os.getppid()}"&del exit_self.bat')
    subprocess.Popen(['start', 'cmd.exe', '/c', 'exit_self.bat'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)


    How can I do it without temp files? What did I miss? For reference, I used /k instead of /c option of cmd.exe to leave the window and check the error message in the window, it is as follows:

    Waiting for 0 seconds, press a key to continue ...
    ERROR: Invalid argument/option - 'eq'.
    Type "TASKKILL /?" for usage.


    I'm not sure if it will help, but I added echo to see the syntax of the command being executed:

    subprocess.Popen(['start', 'cmd.exe', '/k', 'echo', f'timeout 5&taskkill /f /fi "PID eq {os.getppid()}"'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)


    The result is:

    "timeout 5&taskkill /f /fi \"PID eq 3988\""

    Continue reading...

Compartilhe esta Página