connect file object to standard output?

B

beliavsky

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
 
D

Diez B. Roggisch

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
 
L

Larry Bates

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
 
S

Scott David Daniels

.... 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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,213
Latest member
DonnellTol

Latest Threads

Top