On 2018-10-24 09:58, f...@lutix.org wrote:
Hello everyone,
I have 2 functions whose aim is to read a pdf file, the first one manages an
uploaded file, the another one fecth a remote one (via an url).
They are quite the same:
########################
def handle_uploaded_file(path,file):
#if os.path.isfile(path + '/' + file.name):
# file.name = '_' + file.name
destination = open(path + '/' + file.name, 'wb+')
for chunk in file.chunks():
destination.write(chunk)
destination.close()
if check_file(path,file.name):
return True
else:
return False
def handle_remote_file(url,path,file_name=''):
if not file_name:
file_name = url.split('/')[-1]
with urllib.request.urlopen(url) as response:
with open(path + '/' + file_name, 'wb+') as out_file:
shutil.copyfileobj(response, out_file)
if check_file(path,file_name):
return True
else:
return False
####################
I am wondering about the way I could rationalize those 2 functions.
I have read about function factory and maybe it crosses my need.
Do you have some advices?
Thanks, best regards
Replace:
destination = open(path + '/' + file.name, 'wb+')
for chunk in file.chunks():
destination.write(chunk)
destination.close()
with:
with open(path + '/' + file.name, 'wb+') as destination:
for chunk in file.chunks():
destination.write(chunk)
--
https://mail.python.org/mailman/listinfo/python-list