make sure files are closed using the with statement

This commit is contained in:
Benjamin Peterson 2009-05-10 02:29:00 +00:00
parent 14c7bc2ad2
commit b91e8ede7a

View file

@ -1047,7 +1047,7 @@ class ZipFile:
self.fp.write(zinfo.FileHeader())
return
fp = open(filename, "rb")
with open(filename, "rb") as fp:
# Must overwrite CRC and sizes with correct data later
zinfo.CRC = CRC = 0
zinfo.compress_size = compress_size = 0
@ -1068,7 +1068,6 @@ class ZipFile:
buf = cmpr.compress(buf)
compress_size = compress_size + len(buf)
self.fp.write(buf)
fp.close()
if cmpr:
buf = cmpr.flush()
compress_size = compress_size + len(buf)
@ -1388,9 +1387,8 @@ def main(args = None):
tgtdir = os.path.dirname(tgt)
if not os.path.exists(tgtdir):
os.makedirs(tgtdir)
fp = open(tgt, 'wb')
with open(tgt, 'wb') as fp:
fp.write(zf.read(path))
fp.close()
zf.close()
elif args[0] == '-c':