Skip to content

Commit

Permalink
fix unclosed file (#513)
Browse files Browse the repository at this point in the history
  • Loading branch information
msperber committed Aug 11, 2018
1 parent e1cb477 commit 7b857ec
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions xnmt/input_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ def read_sent(self, line: str, idx: int) -> sent.Sentence:
@lru_cache(maxsize=128)
def count_sents(self, filename):
newlines = 0
f = open(filename, 'r+b')
for line in f:
newlines += 1
with open(filename, 'r+b') as f:
for _ in f:
newlines += 1
return newlines

def iterate_filtered(self, filename, filter_ids=None):
Expand Down Expand Up @@ -383,9 +383,9 @@ def read_sents(self, filename, filter_ids=None):
npzFile.close()

def count_sents(self, filename):
npzFile = np.load(filename, mmap_mode="r") # for counting sentences, only read the index
l = len(npzFile.files)
npzFile.close()
npz_file = np.load(filename, mmap_mode="r") # for counting sentences, only read the index
l = len(npz_file.files)
npz_file.close()
return l

class IDReader(BaseTextReader, Serializable):
Expand Down

0 comments on commit 7b857ec

Please sign in to comment.