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)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…