I am using a C library (tesseract) from my Python code. The C library has print statements like these that show up in my python program's stdout:
tprintf("Too few characters. Skipping this page\n");
Is there a way to suppress these print statements when using the C library from python? I've tried an approach I found on SO but it doesn't help. It does suppress print "test"
, however, it doesn't suppress the output coming from tprintf
in the C library.
I've tried the solution mention here but it doesn't work. I still see output on the stdout.
devnull = open('/dev/null', 'w')
oldstdout_fno = os.dup(sys.stdout.fileno())
os.dup2(devnull.fileno(), 1)
tesseract = ctypes.cdll.LoadLibrary("/path/to/tesseract.so")
api = tesseract.TessBaseAPICreate()
rc = tesseract.TessBaseAPIInit3(api, TESSDATA_PREFIX, "osd")
tesseract.TessBaseAPISetPageSegMode(api, 1) #this line prints "Too few characters. Skipping this page" to stdout
os.dup2(oldstdout_fno, 1)