]> granicus.if.org Git - python/commitdiff
Improvements to copyfile(): open the files in binary mode, and close
authorGuido van Rossum <guido@python.org>
Tue, 29 Apr 1997 13:08:15 +0000 (13:08 +0000)
committerGuido van Rossum <guido@python.org>
Tue, 29 Apr 1997 13:08:15 +0000 (13:08 +0000)
them in a finally clause.

Lib/shutil.py

index dba4ee161c508d847df3f2796d6f8193c5e2440e..1a5a6f1ccfab7bdf9bb70baf08f7e64f87291e31 100644 (file)
@@ -1,4 +1,5 @@
 # Module 'shutil' -- utility functions usable in a shell-like program
+# XXX The copy*() functions here don't copy the data fork on Mac
 
 import os
 
@@ -8,12 +9,21 @@ MODEBITS = 010000     # Lower 12 mode bits
 # Copy data from src to dst
 #
 def copyfile(src, dst):
-       fsrc = open(src, 'r')
-       fdst = open(dst, 'w')
-       while 1:
-               buf = fsrc.read(16*1024)
-               if not buf: break
-               fdst.write(buf)
+       fsrc = None
+       fdst = None
+       try:
+               fsrc = open(src, 'rb')
+               fdst = open(dst, 'wb')
+               while 1:
+                       buf = fsrc.read(16*1024)
+                       if not buf:
+                               break
+                       fdst.write(buf)
+       finally:
+               if fdst:
+                       fdst.close()
+               if fsrc:
+                       fsrc.close()
 
 # Copy mode bits from src to dst
 #