473,549 Members | 2,209 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2742
>>> import sys
sys.stdout = file("output"," w")
print "here you go"


May 11 '06 #2
Harold Fellermann wrote:
import sys
sys.stdou t = 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.jav a;
$> 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
12693
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 script to a file (including that which is printed by the spawned programs) via the redirect (">") function on a Win2K Command Prompt. In the...
3
3744
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 problem is the C++ lib does write stuffs on its stdout, and I would like to print those messages in a Tk frame. When I run the computation, it has...
9
1755
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 that could import sys afterwards - so I don't want to make the change in the sys module.
8
5768
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 technically create an executable that calls the function and then using a command prompt call the exexutable using the > output.txt redirector but I...
3
2102
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. open(STDOUT, ">flat.log") || die "Can't redirect stdout"; prinf " ----------------------------------------------\n";
5
2598
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 sys.stdout = self
17
9448
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 redirect: Response.Redirect("/website/pdffiles/myreport.pdf"); Response.End; IE opens a "File Download" box and if you click the "open" button...
0
1818
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 did not require redirection due to the fact that every X11 application can have an STDOUT associated with it. I then proceeded to take interest in...
2
10051
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 work: #!/usr/bin/python import subprocess as proc import tempfile name = tempfile.NamedTemporaryFile(mode='w+b') print 'name is '+ name.name
0
7548
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7743
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7504
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5391
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3499
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
786
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.