[Python-Dev] File Object Copy
Wed, 12 Jul 2000 09:13:32 +0200 (MEST)
> I've found a need for the following function:
> def copyfile(fin, fout):
> while 1:
> buf = fin.read(4096)
> if not buf:
> In several different places (modules I've written, SimpleHTTPServer uses
> it, urllib has that code inlined somewhere, etc.)
> (Of course, 4096 should be a parameter, but that's nits)
> Can anyone suggest a module in which this function would belong?
A very similar function lives at a somewhat hidden flat in the third
floor of the standard library building. ;-)
It is called 'shutil.copyfile'.
See '......html/lib/module-shutil.html' for more information.
'shutil.copyfile' however takes filenames as arguments and contains
the loop sketched above as inline code. I see two possibilities:
1. It would be easy to skip the open and close operations in
'shutil.copyfile', if either 'src' or 'dst' are already open
file objects instead of strings.
2. Or you factor out the copy loop operation above and rename it into
'copycontent' or 'copybytes' or whatever.