12

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
12
  • 1
    How do you know the program has been written in C and not in C++, Pascal, Modula, Fortran, Cobol or Brainfuck? Commented Feb 19, 2016 at 0:09
  • Or any other language that compiles to .exe Commented Feb 19, 2016 at 0:40
  • Please provide a traceback. What happend when you run this code outside of IPython? Commented Feb 19, 2016 at 0:41
  • 1- use raw string literal for Windows paths (otherwise the backslash may have a special meaning inside a literal): r'C:\Users\..' 2- to discard possible input, use stdin=subprocess.DEVNULL Commented Feb 19, 2016 at 14:02
  • 1
    Update. I don't think this was a file descriptor error. i think this is a working directory error. The cwd of the subprocess shows as "/cygdrive/c/Working/Python" when it needs to be "C:/Working/Python". Commented Feb 22, 2016 at 22:08

2 Answers 2

7

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

Sign up to request clarification or add additional context in comments.

Comments

-6

Try this

import subprocess

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.