473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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_console(file):
def write(self, s):
file.write(self, s)
print s,
>>f = File_and_console('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_console.write

Why? How can I fix it?
Thanks
iu2
Jan 15 '08 #1
3 1172
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_console():
def __init__(self, *args):
self.fileobj = open(*args)
def write(self, s):
self.fileobj.write(s)
print s,

f = File_and_console('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_console(file):
def write(self, s):
file.write(self, s)
print s,
>f = File_and_console('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_console.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....@gmail.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_console():
* * * * def __init__(self, *args):
* * * * * * * * self.fileobj = open(*args)
* * * * def write(self, s):
* * * * * * * * self.fileobj.write(s)
* * * * * * * * print s,

f = File_and_console('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_console(file):
def write(self, s):
file.write(self, s)
print s,
>>>f = File_and_console('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(object):

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***********@Acm.Org

Jan 16 '08 #4

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

Similar topics

9
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
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
isladogs
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...

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.