Redirecting output to a file

I’m trying to redirect the output of some commands to a file like this:

f=open(“out.txt”,“w”)
print >>f, render.ls()
or
print >>f,cvMgr

But it doesn’t work. Text appears on the console but it’s some sort of side-effect.

How can we solve this situation? Thanks in advance.

Try:

strm = StringStream()
render.ls(strm)
open('out.txt', 'w').write(strm.getData())

Or:

strm = StringStream()
cvMgr.write(strm)
open('out.txt', 'w').write(strm.getData())

David

Do you mind that i add this to your wiki into a new appendix? Maybe something called “Forum Highlights”. This way i won’t forget this information.

There was also a way to specify the file to write to in the config file.

Yes. You can put:
notify-output out.txt
to write all Panda output to out.txt. It’s also possible to do this on a one-at-a-time basis at runtime with some careful low-level calls into Notify.

There are lots of ways to achieve this. The StringStream approach is just one quick-and-dirty answer.

David

Here’s an approach that doesn’t require saving the entire output in RAM (the output is directly written to the named file). Especially important if the output might be very large:

strm = MultiplexStream()
strm.addFile(Filename('out.txt'))
render.ls(strm)

David

Thanks. I have a request, for the manual. Is that if someone could add a chapter summarizing all about Panda input/output classes and related stuff like filenames, zip archives, net i/o, and notifications.

The manual is a wiki. You can add stuff yourself, if you like.

I don’t see any edit links in there like the other wikis.

PS: Never mind, i found how it is done.

I have updated the FAQ page on the manual with the info on this thread and a few more things i asked before:

panda3d.org/manual/index.php/FAQ

There may be some syntax errors because i’m not an English native speaker.