Skip to content

Commit 587d6a9

Browse files
committed
Use the with context manager to close files.
Make sure file handles get closed when done with.
1 parent 2a0122a commit 587d6a9

File tree

5 files changed

+26
-14
lines changed

5 files changed

+26
-14
lines changed

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ def getversioncfg():
6363
cp.set('DEFAULT', 'commit', g['commit'])
6464
cp.set('DEFAULT', 'date', g['date'])
6565
cp.set('DEFAULT', 'timestamp', g['timestamp'])
66-
cp.write(open(versioncfgfile, 'w'))
66+
with open(versioncfgfile, 'w') as fp:
67+
cp.write(fp)
6768
return cp
6869

6970
versiondata = getversioncfg()

src/diffpy/Structure/Parsers/structureparser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ def parseFile(self, filename):
7373
Raise StructureFormatError or IOError.
7474
"""
7575
self.filename = filename
76-
f = open(filename)
77-
s = f.read()
76+
with open(filename) as fp:
77+
s = fp.read()
7878
stru = self.parse(s)
7979
return stru
8080

src/diffpy/Structure/tests/TestP_cif.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ def tearDown(self):
7878
def test_parse(self):
7979
"""check P_cif.parse()
8080
"""
81-
sgood = open(self.goodciffile).read()
82-
sbad = open(self.badciffile).read()
81+
with open(self.goodciffile) as fp1:
82+
sgood = fp1.read()
83+
with open(self.badciffile) as fp2:
84+
sbad = fp2.read()
8385
pfile, ptest = self.pfile, self.ptest
8486
stru_check = pfile.parseFile(self.goodciffile)
8587
stru = ptest.parse(sgood)
@@ -95,8 +97,10 @@ def test_parse(self):
9597
def test_parseLines(self):
9698
"""check P_cif.parseLines()
9799
"""
98-
goodlines = open(self.goodciffile).readlines()
99-
badlines = open(self.badciffile).readlines()
100+
with open(self.goodciffile) as fp1:
101+
goodlines = fp1.readlines()
102+
with open(self.badciffile) as fp2:
103+
badlines = fp2.readlines()
100104
pfile, ptest = self.pfile, self.ptest
101105
stru_check = pfile.parseFile(self.goodciffile)
102106
stru = ptest.parseLines(goodlines)

src/diffpy/Structure/tests/TestP_discus.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ def test_ignored_lines(self):
9797
"""
9898
r1 = 'ignored record 1\n'
9999
r2 = 'ignored record 2\n'
100-
ni_lines = open(datafile('Ni-discus.stru')).readlines()
100+
with open(datafile('Ni-discus.stru')) as fp:
101+
ni_lines = fp.readlines()
101102
ni_lines.insert(2, r1)
102103
ni_lines.insert(4, r2)
103104
s_s1 = "".join(ni_lines)
@@ -125,7 +126,8 @@ def test_spdiameter_parsing(self):
125126
stru13 = Structure()
126127
stru13.readStr(s13)
127128
self.assertEqual(13, stru13.pdffit['spdiameter'])
128-
ni_lines = open(datafile('Ni.stru')).readlines()
129+
with open(datafile('Ni.stru')) as fp:
130+
ni_lines = fp.readlines()
129131
ni_lines.insert(3, 'shape invalid, 7\n')
130132
sbad = ''.join(ni_lines)
131133
self.assertRaises(StructureFormatError, self.stru.readStr,
@@ -148,7 +150,8 @@ def test_stepcut_parsing(self):
148150
stru13 = Structure()
149151
stru13.readStr(s13)
150152
self.assertEqual(13, stru13.pdffit['stepcut'])
151-
ni_lines = open(datafile('Ni.stru')).readlines()
153+
with open(datafile('Ni.stru')) as fp:
154+
ni_lines = fp.readlines()
152155
ni_lines.insert(3, 'shape invalid, 7\n')
153156
sbad = ''.join(ni_lines)
154157
self.assertRaises(StructureFormatError, self.stru.readStr,

src/diffpy/Structure/tests/TestP_pdffit.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ def test_writeStr_pdffit(self):
153153
"""check writing of normal xyz file"""
154154
stru = self.stru
155155
stru.read(datafile('Ni.stru'), self.format)
156-
f_s = open(datafile('Ni.stru')).read()
156+
with open(datafile('Ni.stru')) as fp:
157+
f_s = fp.read()
157158
f_s = re.sub('[ \t]+', ' ', f_s)
158159
f_s = re.sub('[ \t]+\n', '\n', f_s)
159160
s_s = stru.writeStr(self.format)
@@ -179,7 +180,8 @@ def test_ignored_lines(self):
179180
"""
180181
r1 = 'ignored record 1'
181182
r2 = 'ignored record 2'
182-
ni_lines = open(datafile('Ni.stru')).readlines()
183+
with open(datafile('Ni.stru')) as fp:
184+
ni_lines = fp.readlines()
183185
ni_lines.insert(2, r1 + '\n')
184186
ni_lines.insert(4, r2 + '\n')
185187
s_s1 = "".join(ni_lines)
@@ -207,7 +209,8 @@ def test_spdiameter_parsing(self):
207209
stru13 = Structure()
208210
stru13.readStr(s13)
209211
self.assertEqual(13, stru13.pdffit['spdiameter'])
210-
ni_lines = open(datafile('Ni.stru')).readlines()
212+
with open(datafile('Ni.stru')) as fp:
213+
ni_lines = fp.readlines()
211214
ni_lines.insert(3, 'shape invalid, 7\n')
212215
sbad = ''.join(ni_lines)
213216
self.assertRaises(StructureFormatError, self.stru.readStr,
@@ -230,7 +233,8 @@ def test_stepcut_parsing(self):
230233
stru13 = Structure()
231234
stru13.readStr(s13)
232235
self.assertEqual(13, stru13.pdffit['stepcut'])
233-
ni_lines = open(datafile('Ni.stru')).readlines()
236+
with open(datafile('Ni.stru')) as fp:
237+
ni_lines = fp.readlines()
234238
ni_lines.insert(3, 'shape invalid, 7\n')
235239
sbad = ''.join(ni_lines)
236240
self.assertRaises(StructureFormatError, self.stru.readStr,

0 commit comments

Comments
 (0)