diff options
author | Guido van Rossum <guido@python.org> | 1997-04-29 13:08:15 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1997-04-29 13:08:15 (GMT) |
commit | 277206b08e2c9476ac51a277c11e04a2c30d5a33 (patch) | |
tree | bd6292e90d3a576cce3712607d9557429689f477 /Lib | |
parent | e9a0732cd1ccda75ad6092f3853e32aa99880003 (diff) | |
download | cpython-277206b08e2c9476ac51a277c11e04a2c30d5a33.zip cpython-277206b08e2c9476ac51a277c11e04a2c30d5a33.tar.gz cpython-277206b08e2c9476ac51a277c11e04a2c30d5a33.tar.bz2 |
Improvements to copyfile(): open the files in binary mode, and close
them in a finally clause.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/shutil.py | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/Lib/shutil.py b/Lib/shutil.py index dba4ee1..1a5a6f1 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -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 # |