473,508 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

connect file object to standard output?

I want to write a function that writes to an output file if specified
and otherwise to standard output. How can I connect a file object to
standard output in the code below? I could use an if statement to
choose between print and print>>fp throughout the function, but this
seems awkward. I think there is a way to connect standard output to a
file, but I'd prefer not to do that, since I want to use plain print
statements to warn about errors in the function and have their output
appear on the screen. Thanks.

def write_data(data,out_file=""):
if (out_file != ""):
fp = open(out_file,"w")
else
fp = # how to connect standard output to fp?
print>>fp,data
# more print>>fp statements follow

May 8 '06 #1
3 1673
be*******@aol.com wrote:
I want to write a function that writes to an output file if specified
and otherwise to standard output. How can I connect a file object to
standard output in the code below? I could use an if statement to
choose between print and print>>fp throughout the function, but this
seems awkward. I think there is a way to connect standard output to a
file, but I'd prefer not to do that, since I want to use plain print
statements to warn about errors in the function and have their output
appear on the screen. Thanks.

def write_data(data,out_file=""):
if (out_file != ""):
fp = open(out_file,"w")
else fp = sys.stdout print>>fp,data
# more print>>fp statements follow

sys.stdout _is_ a file...

DIez
May 8 '06 #2
be*******@aol.com wrote:
I want to write a function that writes to an output file if specified
and otherwise to standard output. How can I connect a file object to
standard output in the code below? I could use an if statement to
choose between print and print>>fp throughout the function, but this
seems awkward. I think there is a way to connect standard output to a
file, but I'd prefer not to do that, since I want to use plain print
statements to warn about errors in the function and have their output
appear on the screen. Thanks.

def write_data(data,out_file=""):
if (out_file != ""):
fp = open(out_file,"w")
else
fp = # how to connect standard output to fp?
print>>fp,data
# more print>>fp statements follow

import sys

def write_data(data, out_file=None):
if out_file is None:
fp = sys.stdout # how to connect standard output to fp?
else
fp = open(out_file,"w")

fp.write(data)
# more fp.write() statements follow
-Larry Bates
May 8 '06 #3
be*******@aol.com wrote:
.... I think there is a way to connect standard output to a
file, but I'd prefer not to do that, since I want to use plain print
statements to warn about errors in the function and have their output
appear on the screen. Thanks.

def write_data(data,out_file=""):
if (out_file != ""):
fp = open(out_file,"w")
else:
fp = # how to connect standard output to fp? fp = None print >>fp, data
# more print>>fp statements follow

If you are only using "print >>x"-style statements, simply
set fp to None (which means "write on stdout"). That makes it
easier to only close the file you opened later., say by ending
the routine above with:

if fp is not None:
fp.close()

--Scott David Daniels
sc***********@acm.org
May 8 '06 #4

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

Similar topics

1
2573
by: Peter Nikolaidis | last post by:
Greetings, I am attempting to get MySQLdb 0.9.2 installed on Mac OS 10.2 with a Fink distribution of Python 2.2.2. I have seen only a few posts on the subject, some of them relate to...
4
6434
by: banz | last post by:
Hello I have a problem to resolve: I wrote a Perlscript which caches data from a server (local on my machine) I would like to have a other connection to a remote server but I don't know how to...
0
3910
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
2
1860
by: iwdu15 | last post by:
hey, i was wondering if 1) anyone could tell me whats wrong with my code, i did a little fixing to the msdn version fo this, or 2) how to make a simple program that will connect and listen for...
2
1683
by: geno | last post by:
Hello, When using VB.Net to connect ot a web service, I am getting the error The underlying connection was closed: Unable to connect to the remote server What could be causing this? ...
14
2779
by: prasadjoshi124 | last post by:
Hi All, I am writing a small tool which is supposed to fill the filesystem to a specified percent. For, that I need to read how much the file system is full in percent, like the output given...
1
2035
by: GNoter | last post by:
Scenario: I've a WebFarm with 2 web servers which are NLBs (network load balanced). Web1 and Web2; they are not part of a domain. I have a third server, Server3, which is part of a domain and on...
14
11622
by: Khan | last post by:
Hello, I want to connect my intranet server (with php/apache) to the other informix database server, But I didnt it. I dont know how i can do it? Can i use php_informix extensions for example...
14
12770
by: =?Utf-8?B?R2lkaQ==?= | last post by:
Hi, In my windows applicationm, i need to excute a batch file. this batch file throws some text and questions to the screen, i need to catch the standard Output, check if it's a question, in...
0
7225
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,...
0
7124
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
7385
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...
1
7046
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
4707
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...
0
3195
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1558
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 ...
0
418
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...

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.