In python3, you have to use print
with a file argument if you want to override stdout as the documentation says and as documented in this question:
Python 2.7: Print to File
This gives you the basic building block for achieve what you're looking for. You're not really explaining anything on how you expect to use it with tinydb...
That said, your print call should look like this:
fake_stdout = open('file', 'w')
print(data, file=fake_stdout)
That's it.
If for some reasons you have a lot of print statement to change, you could do something like this:
Have a module with a thread local variable that get initialized. Here look at this question: Thread local storage in Python
Then when you have your module set up you can add this statement in all your files that needs a different print stdout
from special_print import local_print as print
All you have to do is to define a threadlocal variable in your special_print module and then define it when a new thread starts with something like this:
def print_factory(fout):
def local_print(arg):
print(arg, file=fout)
return local_print
In each new thread you run something like this:
import special_print
special_print.local_print = print_factory(fake_stdout)
Then when you import this print
method in each of your threads, they'll correctly output the data to the fake_stdout you defined in each thread.