python - How to use subprocess.run when the subprocess requires an <ENTER> to complete - Stack Overflow

admin2025-04-29  2

I have an windows console executable provided by a third party. It's a file converter and requires an input and output file. On completion it has a "Press any key to exit" prompt. I'm trying to run it from within my program using subprocess.run. It works but it shows the prompt and an unhandled exception error in the console.

def load_mydata(self, file):
    _output = file.with_suffix(".csv")
    # delete output file if it already exists
    _output.unlink(missing_ok=True)
    _args = 'MyDataConverter.exe input="{}" output="{}"'.format(file, _output)
    print(_args)
    subprocess.run(_args)
    print('tada')

returns this between the print statements:

Press any key to exit.
Unhandled Exception: System.InvalidOperationException: Cannot read keys when either application does not have a console or when console input has been redirected from a file. Try Console.Read.
       at System.Console.ReadKey(Boolean intercept)
       at Converter.Program.ProgramExit(TextWriter output, Int32 returnValue)
       at Converter.Program.Main(String[] args)

I've tried this:

subprocess.run(_args, input=b'\n')

and

with open('MyResponsefile.txt', 'r') as fIn:
    subprocess.run(_args, stdin=fIn)

where the response file just contained a character and a newline.

I even tried

try:
    subprocess.run(_args)
except:
    pass

but I get the same result every time.

I have an windows console executable provided by a third party. It's a file converter and requires an input and output file. On completion it has a "Press any key to exit" prompt. I'm trying to run it from within my program using subprocess.run. It works but it shows the prompt and an unhandled exception error in the console.

def load_mydata(self, file):
    _output = file.with_suffix(".csv")
    # delete output file if it already exists
    _output.unlink(missing_ok=True)
    _args = 'MyDataConverter.exe input="{}" output="{}"'.format(file, _output)
    print(_args)
    subprocess.run(_args)
    print('tada')

returns this between the print statements:

Press any key to exit.
Unhandled Exception: System.InvalidOperationException: Cannot read keys when either application does not have a console or when console input has been redirected from a file. Try Console.Read.
       at System.Console.ReadKey(Boolean intercept)
       at Converter.Program.ProgramExit(TextWriter output, Int32 returnValue)
       at Converter.Program.Main(String[] args)

I've tried this:

subprocess.run(_args, input=b'\n')

and

with open('MyResponsefile.txt', 'r') as fIn:
    subprocess.run(_args, stdin=fIn)

where the response file just contained a character and a newline.

I even tried

try:
    subprocess.run(_args)
except:
    pass

but I get the same result every time.

Share Improve this question edited Jan 7 at 15:01 marcp asked Jan 6 at 23:23 marcpmarcp 1,2272 gold badges18 silver badges39 bronze badges 1
  • input=b'\n' should have worked. – Barmar Commented Jan 7 at 0:59
Add a comment  | 

1 Answer 1

Reset to default 0

Trial and error save the day...

The following code executes without the prompt or the error:

subprocess.run(_args, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

The stdout suppresses the prompt and stderr suppresses the error message.

I expect this would have been intuitive for linux users, but for a windows-head such as myself it took a lot of effort, so I'm including it here.

转载请注明原文地址:http://anycun.com/QandA/1745939637a91401.html