471,092 Members | 1,390 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 1565
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Peter Nikolaidis | last post: by
2 posts views Thread by iwdu15 | last post: by
14 posts views Thread by Khan | last post: by
14 posts views Thread by =?Utf-8?B?R2lkaQ==?= | last post: by

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.