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 1692 be*******@aol.c om 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.c om 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.c om 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***********@a cm.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 "conflicting header files," but I don't
know what to do about conflicting header files, or where I would find
them, and once I found them, which ones to remove.
I have compiled MySQL 4.1 and installed into /usr/local/mysql, but
since have moved to a Fink...
|
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 define the servername / hostname in
my Perl Progrem..
Here is the code:
|
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. It is
almost like it is trying to implement it's own COM interfaces...
below is the header, and a link to the dll+code:
Zip file with header, example, and DLL:...
|
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 connections async. if the user
pushes the selected buttons.
i just want a program that connects to another computer on the port and IP
address selected and if the other computer is listening, then connect to it
but this code doesnt work :
Imports...
|
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?
Thanks!
| |
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 df -k
lopgod10:~/mycrfile # df -k /mnt/mvdg1/vset
Filesystem 1K-blocks Used Available Use% Mounted on
|
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 the same physical network, and it has an MSAccess database which is used on the external webfarm as well as on the internal intranet. I can connect via the intranet because the DB file is on the same box from which the intranet is being served...
|
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 ifx_connect();
I was read this article: http://www-128.ibm.com/developerworks/db2/library/techarticle/dm-0606bombardier/index.html
and doing step one step but i was not success.
|
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 case it's a question,
i want to popup a messageBox or something, and bring back to the batch file
the result (Yes\No question). I know how to excute the batch file and get all
the Standard output at the end, but i don't know who can i read it line by...
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |