diff options
author | Kyle Mckay <kyle.mckay@codethink.co.uk> | 2021-11-04 09:44:55 +0000 |
---|---|---|
committer | Kyle Mckay <kyle.mckay@codethink.co.uk> | 2021-11-16 11:17:05 +0000 |
commit | 7d85bd957e72e2669e2b20364275dc248df26328 (patch) | |
tree | e057e0ceb2689b362bbc4d69bf9fc4927364322b /lorry | |
parent | 36c5fb0d66737274565035c0a7a1791801110c2d (diff) | |
download | lorry-7d85bd957e72e2669e2b20364275dc248df26328.tar.gz |
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)
Diffstat (limited to 'lorry')
-rwxr-xr-x | lorry | 5 |
1 files changed, 2 insertions, 3 deletions
@@ -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: |