Skip to content

Commit c4139ea

Browse files
committed
Use "with" statement where possible
1 parent ac88d68 commit c4139ea

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

webware/MiscUtils/PickleCache.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ def read(self, filename,
9292
if not os.path.exists(filename):
9393
if v:
9494
print(f'Cannot find {filename!r}.')
95-
open(filename, 'rb') # to get a properly constructed IOError
95+
with open(filename, 'rb'):
96+
pass # to get a properly constructed IOError
9697

9798
shouldDeletePickle = False
9899
data = None

webware/PSP/Examples/View.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ def writeContent(self):
2626
self.write('<p style="color:red">'
2727
f'No such file {basename} exists</p>')
2828
return
29-
text = open(filename, encoding='utf-8').read()
29+
with open(filename, encoding='utf-8') as pspFile:
30+
text = pspFile.read()
3031
text = self.htmlEncode(text)
3132
text = text.replace('\n', '<br>').replace('\t', ' '*4)
3233
self.write(f'<pre>{text}</pre>')

webware/Scripts/MakeAppWorkDir.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ def makeDirectories(self):
116116
self.msg(f"\t{path} already exists.")
117117
else:
118118
os.makedirs(path)
119-
open(os.path.join(path, '__init__.py'), 'w',
120-
encoding='ascii').write('#\n')
119+
with open(os.path.join(path, '__init__.py'), 'w',
120+
encoding='ascii') as initFile:
121+
initFile.write('#\n')
121122
self.msg(f"\t{path} created.")
122123

123124
def copyConfigFiles(self):
@@ -193,14 +194,16 @@ def makeDefaultContext(self):
193194
self.msg(f"\t{filename} already exists.")
194195
else:
195196
self.msg(f"\t{filename}")
196-
open(filename, "w", encoding='ascii').write(source)
197+
with open(filename, "w", encoding='ascii') as sourceFile:
198+
sourceFile.write(source)
197199
self.msg("Updating config for default context...")
198200
filename = os.path.join(self._workDir, 'Configs', 'Application.config')
199201
self.msg(f"\t{filename}")
200-
content = open(filename, encoding='utf-8').readlines()
202+
with open(filename, encoding='utf-8') as configFile:
203+
configLines = configFile.readlines()
201204
foundContext = 0
202205
with open(filename, 'w', encoding='utf-8') as output:
203-
for line in content:
206+
for line in configLines:
204207
contextName = self._contextName
205208
if line.startswith(
206209
f"Contexts[{contextName!r}] = {configDir!r}\n"):

0 commit comments

Comments
 (0)