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.
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.
input=b'\n'
should have worked. – Barmar Commented Jan 7 at 0:59