
4 Sep
2014
4 Sep
'14
9:58 a.m.
On 04-09-2014 06:25, lvroyce@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.