]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
apply patch #100868 from Moshe Zadka:
authorGreg Stein <gstein@lyra.org>
Wed, 12 Jul 2000 09:55:30 +0000 (09:55 +0000)
committerGreg Stein <gstein@lyra.org>
Wed, 12 Jul 2000 09:55:30 +0000 (09:55 +0000)
    refactor the copying of file data. new: shutil.copyfileobj(fsrc, fdst)

Lib/shutil.py

index 88abd1095c08356f1ef71c6eb8ab69afee3c9247..dfde2361369ff0d7fbdf8f4909c3d13cb4ce6341 100644 (file)
@@ -9,6 +9,15 @@ import sys
 import stat
 
 
+def copyfileobj(fsrc, fdst, length=16*1024):
+    """copy data from file-like object fsrc to file-like object fdst"""
+    while 1:
+        buf = fsrc.read(length)
+        if not buf:
+            break
+        fdst.write(buf)
+
+       
 def copyfile(src, dst):
     """Copy data from src to dst"""
     fsrc = None
@@ -16,11 +25,7 @@ def copyfile(src, dst):
     try:
         fsrc = open(src, 'rb')
         fdst = open(dst, 'wb')
-        while 1:
-            buf = fsrc.read(16*1024)
-            if not buf:
-                break
-            fdst.write(buf)
+        copyfileobj(fsrc, fdst)
     finally:
         if fdst:
             fdst.close()