From 112f4be3825676f5240511a1f0344a1513a2b8ac Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Mon, 5 Jul 2021 12:48:20 +0100 Subject: [PATCH] Use with when opening files --- content.py | 7 ++----- posts.py | 4 +--- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/content.py b/content.py index 85f233767..6bf97dc80 100644 --- a/content.py +++ b/content.py @@ -995,11 +995,8 @@ def saveMediaInFormPOST(mediaBytes, debug: bool, if os.path.isfile(possibleOtherFormat): os.remove(possibleOtherFormat) - fd = open(filename, 'wb') - if not fd: - return None, None - fd.write(mediaBytes[startPos:]) - fd.close() + with open(filename, 'wb') as fp: + fp.write(mediaBytes[startPos:]) if not os.path.isfile(filename): print('WARN: Media file could not be written to file: ' + filename) diff --git a/posts.py b/posts.py index fa0f2c682..a213f7444 100644 --- a/posts.py +++ b/posts.py @@ -1348,11 +1348,9 @@ def getPinnedPostAsJson(baseDir: str, httpPrefix: str, pinnedPostJson = {} actor = httpPrefix + '://' + domainFull + '/users/' + nickname if os.path.isfile(pinnedFilename): - pinFile = open(pinnedFilename, "r") pinnedContent = None - if pinFile: + with open(pinnedFilename, "r") as pinFile: pinnedContent = pinFile.read() - pinFile.close() if pinnedContent: pinnedPostJson = { 'atomUri': actor + '/pinned',