python - prevent IOError: [Errno 5] Input/output error when running without stdout
<p>I have a script that runs automatically on server through cronjob and it import and run several other scripts.</p>

<p>Some of them use prints, which naturally creates <code>IOError: [Errno 5] Input/output error</code> because the script runs without any SSH / terminal connected, so there's no proper stdout setup.</p>

<p>There are lots of questions about this subject but I couldn't find anyone that actually <strong>solve</strong> it, assuming I can't remove the print or change the executed scripts.</p>

<p>I tried several things, including:</p>

<pre><code>class StdOut(object):
    def __init__(self):
        pass
    def write(self, string):
        pass
sys.stdout = StdOut()
sys.stderr = StdOut()
</code></pre>

<p>and </p>

<pre><code>from __future__ import print_function
import __builtin__

def print(*args, **kwargs):
        pass
    __builtin__.print = print
</code></pre>

<p>But none of it works. I assume it only affect the module itself and not the modules I import / run later.</p>

<p>So how can I create a stub stdout that will affect <strong>all</strong> modules in the process? Like I said, I don't want to change the scripts that are executed from the main module, but I can change everything inside the importing module. And just to clearify - everything is imported, no new processes are spawned etc.</p>

<p>Thanks,</p>