I am new to python n developing scripts as a part of my project.I need help regarding HOW THE OUTPUT OF THE SCRIPT CAN BE REDIRECTED TO A TEXT FILE(FOR MAINTAINING ERROR LOG), WHILE ALSO PRINTING IT ON CONSOLE??

Thanks for all the help in advance and I am looking forward for it

Recommended Answers

All 10 Replies

It's not really Python-specific, but I would use the 'tee' command here. On *nix, the invocation of the script would be

python myscript.py | tee out.txt

I'm sure Windows has a similar command as well.

Hope this helps.

You can consider the 'logging' module.

You want all the output in text file, or only the error output?

Wich OS? Are you using GUI?

Cheers and Happy coding

Thanx for the replies..I want all the output to text file meanwhile being displayed on console..am using LINUX. I am supposed to write a main script, which in turn invokes many other scripts n the output from all the scripts should be logged.How can this be done?

To redefine my query, how to write a generic script, which is invoked by all other scripts, which helps in keeping a log of output from all the scripts?

You can create a script, wich loads your scripts with subprocess, and then you can redirect the output, like this you won't need to adjust the scripts.

More than that, you can write a module or use logging as told, but for that you must adjust your acripts to.

Cheers and Happy coding


Cheers and Happy coding

Thanx a lot for ur reply...but, could u pls explain me what is that "adjusting of scripts?"

You can create a script, wich loads your scripts with subprocess, and then you can redirect the output, like this you won't need to adjust the scripts.

More than that, you can write a module or use logging as told, but for that you must adjust your acripts to.

Cheers and Happy coding


Cheers and Happy coding

One more doubt..is it possible to duplicate the output using 'loggigng' i.e, both display it on console n redirect it to log file?

pls bear with me if u feel my queries r silly:) am quite new to python

1 - I mean that if you use subprocess, you don't have to edit or prepare the scripts, you can just load them as they are and redirect the output, with logging the scripts must be prepared to use the module and make the output.

2 - Yes you can make the two, it's a mather of implementation.

Cheers and Happy coding

f=open("myfile.txt","w")
os.dup2(sys.stdout,f)
os.dup2(sys.stderr,f)

is the above right method for duplicating file descriptors? that is duplicating output to both console and log file?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.