Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subprocess call on python gives Error "Bad file descriptor"

Whenever i call a c code executable from Python using the method below I get a "Bad file descriptor" error. When I run the code from the command prompt it works fine. Help please!?

import subprocess

p = subprocess.call(['C:\Working\Python\celp.exe', '-o', 'ofile'], stdin=subprocess.PIPE, stderr=subprocess.STDOUT)

In [84]: %run "C:\Working\Python\test.py"
Error: : Bad file descriptor
like image 612
sciguy Avatar asked Feb 18 '16 23:02

sciguy


People also ask

What is bad file descriptor?

In general, when "Bad File Descriptor" is encountered, it means that the socket file descriptor you passed into the API is not valid, which has multiple possible reasons: The fd is already closed somewhere.

What does subprocess call do in Python?

The Python subprocess call() function returns the executed code of the program. If there is no program output, the function will return the code that it executed successfully. It may also raise a CalledProcessError exception.

What is subprocess Check_output in Python?

The subprocess. check_output() is used to get the output of the calling program in python. It has 5 arguments; args, stdin, stderr, shell, universal_newlines. The args argument holds the commands that are to be passed as a string.


1 Answers

You forgot to add the stdout flag. add stdout = subprocess.PIPE, like this:

p = subprocess.call(
    ['C:\Working\Python\celp.exe', '-o', 'ofile'],
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,  # needed for the next line to be sensible
    stderr=subprocess.STDOUT,
)

now, try run your script again

like image 147
dvir Avatar answered Sep 18 '22 13:09

dvir