[Kimchi-devel] [PATCHv4 5/7] Storage volume upload: Support file based upload
Crístian Viana
vianac at linux.vnet.ibm.com
Thu Sep 4 14:58:14 UTC 2014
On 04-09-2014 06:25, lvroyce at linux.vnet.ibm.com wrote:
> + f = open(file_path, 'wb')
> + while True:
> + data = upload_file.file.read(8192)
> + if not data:
> + break
> + f.write(data)
> + f.close()
The code above may leak the file "f" if an exception is raised while
reading its contents. You must make sure the method "close" will be
executed.
I suggest the following pattern:
with open(file_path, 'wb') as f:
# handle file f
When the "with" block ends, the file be closed whether an exception was
raised or not. You don't need to call "close" explicitly.
More information about the Kimchi-devel
mailing list