Suppressing output of module calling outside library

后端 未结 4 2123
花落未央
花落未央 2020-12-02 00:01

I have an annoying problem when using machine learning library PyML. PyML uses libsvm to train the SVM classifier. The problem is that libsvm outputs some text to standard o

4条回答
  •  北海茫月
    2020-12-02 01:05

    Open /dev/null for writing, use os.dup() to copy stdout, and use os.dup2() to copy your open /dev/null to stdout. Use os.dup2() to copy your copied stdout back to the real stdout after.

    devnull = open('/dev/null', 'w')
    oldstdout_fno = os.dup(sys.stdout.fileno())
    os.dup2(devnull.fileno(), 1)
    makesomenoise()
    os.dup2(oldstdout_fno, 1)
    

提交回复
热议问题