Fixes issue 1571:

Adding missing close for open.
If the "close()" call is missing after a "open(filename)" call, the filename isn't guaranteed to be closed before the interpreter exits.
This is generally a bad practice as explained here: https://stackoverflow.com/questions/7395542/is-explicitly-closing-files-important

Also replaced "fid=open(filename) fid.close()" statements for files with the safer
"with open(filename) as fid:" blocks. See https://www.python.org/dev/peps/pep-0343/
This commit is contained in:
Per A. Brodtkorb
2020-03-23 17:16:44 +01:00
parent 8e2627e8e3
commit e4e8bf8317
38 changed files with 230 additions and 266 deletions

View File

@@ -76,21 +76,17 @@ class DoodleFrame(wx.Frame):
# size events using this sizer.
self.SetSizer(box)
def SaveFile(self):
if self.filename:
data = self.doodle.GetLinesData()
f = open(self.filename, 'wb')
pickle.dump(data, f)
f.close()
with open(self.filename, 'wb') as f:
pickle.dump(data, f)
def ReadFile(self):
if self.filename:
try:
f = open(self.filename, 'rb')
data = pickle.load(f)
f.close()
with open(self.filename, 'rb') as f:
data = pickle.load(f)
self.doodle.SetLinesData(data)
except pickle.UnpicklingError:
wx.MessageBox("%s is not a doodle file." % self.filename,

View File

@@ -258,9 +258,8 @@ def Read_MapGen(filename,stats = False):
"""
from numpy import array
file = open(filename,'rt')
data = file.readlines()
data = [s.strip() for s in data]
with open(filename,'rt') as file_:
data = [s.strip() for s in file_.readlines()]
Shorelines = []
segment = []
@@ -279,21 +278,11 @@ def Read_MapGen(filename,stats = False):
NumPoints = NumPoints + len(segment)
AvgPoints = NumPoints / NumSegments
print("Number of Segments: ", NumSegments)
print("Average Number of Points per segment: ",AvgPoints)
print("Average Number of Points per segment: ", AvgPoints)
return Shorelines
if __name__ == "__main__":
if __name__ == "__main__":
app = DemoApp(0)
app.MainLoop()

View File

@@ -50,37 +50,36 @@ class BNAData:
def Save(self, filename = None):
if not filename:
filename = self.filename
file = open(filename, 'w')
for i, points in enumerate(self.PointsData):
file.write('"%s","%s", %i\n'%(self.Names[i],self.Types[i],len(points) ) )
for p in points:
file.write("%.12f,%.12f\n"%(tuple(p)))
with open(filename, 'w') as file:
for i, points in enumerate(self.PointsData):
file.write('"%s","%s", %i\n'%(self.Names[i],self.Types[i],len(points) ) )
for p in points:
file.write("%.12f,%.12f\n"%(tuple(p)))
def Load(self, filename):
#print("Loading:", filename)
file = open(filename,'rU')
self.Filename = filename
self.PointsData = []
self.Names = []
self.Types = []
while 1:
line = file.readline()
if not line:
break
line = line.strip()
Name, line = line.split('","')
Name = Name[1:]
Type,line = line.split('",')
num_points = int(line)
self.Types.append(Type)
self.Names.append(Name)
polygon = N.zeros((num_points,2),N.float)
for i in range(num_points):
polygon[i,:] = map(float, file.readline().split(','))
self.PointsData.append(polygon)
file.close()
with open(filename,'rU') as file_:
for line in file_:
if not line:
break
line = line.strip()
Name, line = line.split('","')
Name = Name[1:]
Type,line = line.split('",')
num_points = int(line)
self.Types.append(Type)
self.Names.append(Name)
polygon = N.zeros((num_points,2),N.float)
for i in range(num_points):
polygon[i,:] = map(float, file_.readline().split(','))
self.PointsData.append(polygon)
return None
class DrawFrame(wx.Frame):

View File

@@ -115,7 +115,8 @@ class PrintFrameworkSample(wx.Frame):
self.tc.SetFont(wx.Font(FONTSIZE, wx.FONTFAMILY_TELETYPE,
wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))
filename = os.path.join(os.path.dirname(__file__), "sample-text.txt")
self.tc.SetValue(open(filename).read())
with open(filename) as fid:
self.tc.SetValue(fid.read())
self.tc.Bind(wx.EVT_SET_FOCUS, self.OnClearSelection)
wx.CallAfter(self.tc.SetInsertionPoint, 0)