Use with when opening files

main
Bob Mottram 2021-07-05 12:48:20 +01:00
parent e8d45565b4
commit 112f4be382
2 changed files with 3 additions and 8 deletions

View File

@ -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)

View File

@ -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',