473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

redirecting print to a a file

Hi,

Can I redirect print output, so it is send to a file, not stdout.

I have a large program and would like to avoid touching hundreds of print's.

Thx, Andy
May 11 '06 #1
7 2738
>>> import sys
sys.stdout = file("output","w")
print "here you go"


May 11 '06 #2
Harold Fellermann wrote:
import sys
sys.stdout = file("output","w")
print "here you go"


And what if I want to still send the output to stdout and just a log it
in the file as well?

A.
May 11 '06 #3
AndyL enlightened us with:
Can I redirect print output, so it is send to a file, not stdout.
Change sys.stdout to a file object.
I have a large program and would like to avoid touching hundreds of
print's.


I can suggest using the logging module instead of print. It's much
more flexible than prints, and well suited for large programs.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
May 11 '06 #4
Sybren Stuvel wrote:
I have a large program and would like to avoid touching hundreds of
print's.

I can suggest using the logging module instead of print. It's much
more flexible than prints, and well suited for large programs.


Thx for the hint. I will look into that.

A.
May 11 '06 #5
AndyL enlightened us with:
And what if I want to still send the output to stdout and just a log
it in the file as well?


$ python some_program.py | tee output.log

Or write a class that has a write() function and outputs to a file and
to the original value of sys.stdout (IIRC that's in sys.__stdout__)

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
May 11 '06 #6
Sybren Stuvel wrote:
AndyL enlightened us with:
And what if I want to still send the output to stdout and just a log
it in the file as well?

$ python some_program.py | tee output.log

Or write a class that has a write() function and outputs to a file and
to the original value of sys.stdout (IIRC that's in sys.__stdout__)

Sybren


Thx again. Python is cool, do that in C++ or Java :-)

A.
May 11 '06 #7
AndyL wrote:
$ python some_program.py | tee output.log

Thx again. Python is cool, do that in C++ or Java :-)


Yes, thats obviously true ;-)

$> javac someprogram.java;
$> java someprogram | tee output.log

$> g++ someother.cpp -o someother
$> ./someother | tee output.log

SCNR,

Mirco
May 11 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Birch | last post by:
I have a python script that uses the print function throughout, and as well uses calls to os.system() to spawn DOS commandline executables. My issue is that I redirect all of the output from this...
3
by: David Douard | last post by:
Hi everybody, let me explain by problem: I am working on an application which consists in a C++ dll (numeric computations) and a Python IHM (Python/Tk), which must run under Linux and win32. My...
9
by: Fuzzyman | last post by:
Hello, I'm trying to redirect standard out in a single namespace. I can replace sys.stdout with a custom object - but that affects all namespaces. There will be code running simultaneously...
8
by: Morpheus | last post by:
I am trying to test a function that outputs text to the standard output, presumably using a cout call. I do not have access to the source, but need to test the output. Is this possible? I can...
3
by: pnsreee | last post by:
Hi All, I am trying to print a log file created in the same script with the code bellow. but Im not getting output on screen. The output file is created using rediretion of output. ...
5
by: =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7z | last post by:
I wanna print the log to both the screen and file, so I simulatered a 'tee' class Tee(file): def __init__(self, name, mode): file.__init__(self, name, mode) self.stdout = sys.stdout...
17
by: mansb2002 | last post by:
Hi, We recently moved our webserver from Win2K to Win2003. The application works fine. Only problem is that when user views a report as a PDF, IE does not show it. The following code is used to...
0
by: Tom Gaudasinski | last post by:
Greetings, I'm trying to redirect python's stdout to another location. The reason for this is that I'm embedding python in an application. Now, originally my code was developed for Linux and that...
2
by: rparimi | last post by:
I am trying to redirect stderr of a process to a temporary file and then read back the contents of the file, all in the same python script. As a simple exercise, I launched /bin/ls but this doesn't...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.