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 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 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 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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....
|
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...
|
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?
...
| |
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
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...
| |