From 7d85bd957e72e2669e2b20364275dc248df26328 Mon Sep 17 00:00:00 2001 From: Kyle Mckay Date: Thu, 4 Nov 2021 09:44:55 +0000 Subject: Use context manager for raw file requests - Automaitcally clean up when done - Improve code readability - Use of parentheses around a with statement is a Python 3.10 change (though supported in 3.9) --- lorry | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'lorry') diff --git a/lorry b/lorry index b38c2ee..cadc0f2 100755 --- a/lorry +++ b/lorry @@ -808,8 +808,8 @@ class Lorry(cliapp.Application): new_files[src] = file_dest self.progress('.. attempting to fetch %s' % basename) try: - with open(file_dest, 'wb') as raw_file: - urlfile = urllib.request.urlopen(url) + with open(file_dest, 'wb') as raw_file, \ + urllib.request.urlopen(url) as urlfile: raw_file.write(urlfile.read()) try: # HTTP dates use (one of) the email date formats @@ -818,7 +818,6 @@ class Lorry(cliapp.Application): urlfile.info()['Last-Modified'])) except (KeyError, ValueError, TypeError): url_date = None - urlfile.close() if url_date: os.utime(file_dest, (url_date, url_date)) except Exception: -- cgit v1.2.1