By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,088 Members | 1,144 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,088 IT Pros & Developers. It's quick & easy.

Subclassing file class

P: n/a

I'm using v2.3, and trying to write to text files, but with a maximum
line length. So, if a line is getting too long, a suitable ' '
character is replaced by a new line. I'm subclassing the file class,
and, as long as I just use the write method, this works fine. But
"print >>" doesn't behave as I want:

class max_width_file(file):
def write(self, txt):
print "In max_width_file's write method."
fout = max_width_file('foo.txt', 'w')
print >> fout, 'bar'
fout.close()

bypasses my write method and prints 'bar' directly to the file. That
isn't the behaviour I inferred from

http://docs.python.org/ref/print.html

How do I intercept "print >>"? Has anyone already implemented this?
And should the documentation be changed?

Any help very welcome.
David

David Vaughan
Structural Analyst
for Gifford

GIFFORD AND PARTNERS LTD
Carlton House, Ringwood Road, Woodlands,
Southampton, SO40 7HT, UNITED KINGDOM

Tel: +44 (0)23 8081 7500 Fax: +44 (0)23 8081 7600
Email: Da***********@gifford.uk.com
Web: http://www.gifford.uk.com

This e-mail and any files transmitted with it are intended solely for
the use of the individual or entity to whom they are addressed and may
be confidential. If you are not the intended recipient or the person
responsible for delivering the e-mail to the intended recipient, be
advised that you have received this e-mail in error and that any use,
dissemination, forwarding, printing or copying of this e-mail is
strictly prohibited.
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"David Vaughan" <Da***********@Gifford.UK.com> writes:
I'm using v2.3, and trying to write to text files, but with a maximum
line length. So, if a line is getting too long, a suitable ' '
character is replaced by a new line. I'm subclassing the file class,
and, as long as I just use the write method, this works fine. But
"print >>" doesn't behave as I want:


You'll almost certainly be happier implementing a 'file-like object'
that does what you want than subclassing file.

Cheers,
mwh

--
Every day I send overnight packages filled with rabid weasels to
people who use frames for no good reason.
-- The Usenet Oracle, Oracularity #1017-1
Jul 18 '05 #2

P: n/a
On Wed, 4 Aug 2004 11:20:20 +0100, rumours say that "David Vaughan"
<Da***********@Gifford.UK.com> might have written:
I'm using v2.3, and trying to write to text files, but with a maximum
line length. [snip]


Apart from any other advices you get, you might want to consider the
textwrap module.
--
TZOTZIOY, I speak England very best,
"Tssss!" --Brad Pitt as Achilles in unprecedented Ancient Greek
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.