--
view archives: http://groups.google.com/group/python_inside_maya
change your subscription settings: http://groups.google.com/group/python_inside_maya/subscribe
why is this forum so slow to update? I posted a topic on Friday and I don't see it till Tuesday morning
and then i replied to this a few hours ago and it doesn't appear....is it just me?
class Tee:
def __init__(self, filename):
log = open(filename, "w")
self.prevfd = os.dup(sys.__stdout__.fileno())
os.dup2(log.fileno(), sys.__stdout__.fileno())
self.prev = sys.__stdout__
sys.__stdout__ = os.fdopen(self.prevfd, "w")
def __exit__(self):
restore()
def restore():
os.dup2(self.prevfd, self.prev.fileno())
sys.__stdout__ = self.prev
tee = Tee("c:\\log.txt")
So at the moment it does what I'm after but it completely redirects the output window and now there isn't any output in the console, I don't know python well enough at the moment to take it further.
Is there a way to keep both?
Thanks guys,
I'm not sure how that python snippet was working for you. It seems kind of broken. Also, its not really doing a tee, because I believe the os.dup2 is actually closing the original __stdout__. So its really only writing to your new file, as you noticed. Also, it looks like he was trying to do a context manager with the __exit__ method, yet no __enter__ method and not using it in a "with" statement, so it basically does nothing right now.It looks to me like sys.__stdout__ is assigned and used differently on a windows machine than on osx, because I don't get the same output flowing to that descriptor that you seem to, but if I had to suggest a version of that code, it might be this:You just assign a file-like interface to __stdout__ with a write() method that writes to both your own file, and the original file descriptor. See if that works for you.
I'm not sure how that python snippet was working for you. It seems kind of broken. Also, its not really doing a tee, because I believe the os.dup2 is actually closing the original __stdout__. So its really only writing to your new file, as you noticed. Also, it looks like he was trying to do a context manager with the __exit__ method, yet no __enter__ method and not using it in a "with" statement, so it basically does nothing right now.It looks to me like sys.__stdout__ is assigned and used differently on a windows machine than on osx, because I don't get the same output flowing to that descriptor that you seem to, but if I had to suggest a version of that code, it might be this:You just assign a file-like interface to __stdout__ with a write() method that writes to both your own file, and the original file descriptor. See if that works for you.
--
Anyone figured out how to redirect that code to sys.stdout so it canr print in script editor?