473,568 Members | 2,986 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

print >> to a derived file

iu2
Hi,

I'm trying to write data to both a file and the console, so I did:

class File_and_consol e(file):
def write(self, s):
file.write(self , s)
print s,
>>f = File_and_consol e('1.txt', 'w')
f.write('hell o')
hello
>>print >>f, 'world'
the 'write' method works, but 'print >>' doesn't, it writes only to
the file. It doesn't actually call File_and_consol e.write

Why? How can I fix it?
Thanks
iu2
Jan 15 '08 #1
3 1179
This might have something to do with the class being derived from file.

I've written it so that it doesn't derive from file, and it works.

class File_and_consol e():
def __init__(self, *args):
self.fileobj = open(*args)
def write(self, s):
self.fileobj.wr ite(s)
print s,

f = File_and_consol e('testout.tmp' ,'w')
f.write('hello' )
print >>f,'hello',
On 1/15/08, iu2 <is*****@elbit. co.ilwrote:
Hi,

I'm trying to write data to both a file and the console, so I did:

class File_and_consol e(file):
def write(self, s):
file.write(self , s)
print s,
>f = File_and_consol e('1.txt', 'w')
f.write('hello ')
hello
>print >>f, 'world'

the 'write' method works, but 'print >>' doesn't, it writes only to
the file. It doesn't actually call File_and_consol e.write

Why? How can I fix it?
Thanks
iu2
--
http://mail.python.org/mailman/listinfo/python-list
Jan 15 '08 #2
iu2
On Jan 15, 12:44*pm, "Ben Fisher" <jamon....@gmai l.comwrote:
This might have something to do with the class being derived from file.

I've written it so that it doesn't derive from file, and it works.

class File_and_consol e():
* * * * def __init__(self, *args):
* * * * * * * * self.fileobj = open(*args)
* * * * def write(self, s):
* * * * * * * * self.fileobj.wr ite(s)
* * * * * * * * print s,

f = File_and_consol e('testout.tmp' ,'w')
f.write('hello' )
print >>f,'hello',
Thanks, but that's what I tried first. Then I though it would be nice
if I could just inherit from 'file' and implement this with less code.
Jan 15 '08 #3
iu2 wrote:
Hi,

I'm trying to write data to both a file and the console, so I did:

class File_and_consol e(file):
def write(self, s):
file.write(self , s)
print s,
>>>f = File_and_consol e('1.txt', 'w')
....

Always use the same method for writing, or you will get weird results.
If you think the above works, try:
for char in 'sample': f.write(char)

class MultiWrite(obje ct):

def write(self, text):
for dest in self._files:
dest.write(text )

def __init__(self, *files):
self._files = files

def close(self):
for dest in self._files:
dest.close()
f1 = MultiWrite(open ('1.txt', 'w'), sys.stdout)

--Scott David Daniels
Sc***********@A cm.Org

Jan 16 '08 #4

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

Similar topics

9
1063
by: eprint10108 | last post by:
* * * Please Read And/Or Print This! * * * Press Keys On Your Keyboard To Print >> June 1, 2004 8:23:46 pm >> http://115639.aceboard.net/forum2.php?rub=158&cat=61&login=115639&page=0#id96 << * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *...
0
2565
by: Ricardo Bessa | last post by:
Hello, I'm using Reporting Services 2005 and i'm trying to print a report using RsClientPrint.Print(); When the report has no parameters there's no problem. But when the report have parameters then after a succesfly print it show's me the error "And error occurred during printing. (0x8007F303). The final result it's almost good, the...
1
1320
xtianixm
by: xtianixm | last post by:
i know using shellexecute will do. but how about i create a file "print.file". when i used shellexecute it doesnt print directly..can anyone help me on this..how can i print a file with an extension of .file regards xtianixm
0
7605
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...
0
7917
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7665
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...
0
6277
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5217
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...
0
3651
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...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
933
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.