Python 3 is killing Python
Chris Angelico
rosuav at gmail.com
Wed Jul 16 07:54:21 EDT 2014
On Wed, Jul 16, 2014 at 9:35 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> What I had in mind was for print to open the file in append mode, write,
> then close the file.
Ahh, okay. Very different from what I thought you were talking about,
and distinctly different in behaviour from REXX :)
In that case, it avoids the problems that I described, at the expense
of being potentially an attractive nuisance - imagine code like this:
for line in open("input.txt"):
print(transform(line), file="output.txt")
Looks like a really nice clean way to process a file, right? But it's
going to be horrendously slow.
Actually, this could be quite reasonably added as a feature of
print(). Could be monkey-patched in fairly easily
_origprint = print
def print(*a, **kw):
if isinstance(kw.get("file"), (str, bytes)):
with open(kw["file"], 'a') as f:
kw["file"] = f
_origprint(*a, **kw)
else:
_origprint(*a, **kw)
And it'd have its uses. The only risk would be if there's a file-like
object that's a subclass of either str or bytes, which admittedly *is*
theoretically possible...
ChrisA
More information about the Python-list
mailing list