[Python] subprocess.Popen giving different results when I run it many times

Discussão em 'Python' iniciado por Stack, Fevereiro 8, 2025.

  1. Stack

    Stack Membro Participativo

    I am running this code

    import subprocess

    with open("test.txt", "r") as file:
    cmd = file.readline().strip()


    print("test")
    print(cmd)

    with subprocess.Popen(
    cmd,
    stdout=subprocess.PIPE,
    stdin=subprocess.DEVNULL,
    stderr=subprocess.DEVNULL,
    shell=True,
    ) as stream:
    # version = stream.communicate()[0].decode()
    print(stream.communicate()[0])


    test.txt is:

    powershell -NoProfile "$ErrorActionPreference='silentlycontinue'; $tmp = (Get-Item -Path "$env:pROGRAMFILES\Google\Chrome\Application\chrome.exe").VersionInfo.FileVersion; if ($tmp) {echo $tmp; Exit;}; $tmp = (Get-Item -Path "${env:pROGRAMFILES(x86)}\Google\Chrome\Application\chrome.exe").VersionInfo.FileVersion; if ($tmp) {echo $tmp; Exit;}; $tmp = (Get-Item -Path "$env:LOCALAPPDATA\Google\Chrome\Application\chrome.exe").VersionInfo.FileVersion; if ($tmp) {echo $tmp; Exit;};"


    and sometimes I get the right result and sometimes I get nothing

    This command should return the current chrome version but it just don't sometimes.

    Continue reading...

Compartilhe esta Página