MCPcopy
hub / github.com/dabeaz/python-cookbook / gen_opener

Function gen_opener

src/4/creating_data_processing_pipelines/example.py:15–28  ·  view source on GitHub ↗

Open a sequence of filenames one at a time producing a file object. The file is closed immediately when proceeding to the next iteration.

(filenames)

Source from the content-addressed store, hash-verified

13 yield os.path.join(path, name)
14
15def gen_opener(filenames):
16 '''
17 Open a sequence of filenames one at a time producing a file object.
18 The file is closed immediately when proceeding to the next iteration.
19 '''
20 for filename in filenames:
21 if filename.endswith('.gz'):
22 f = gzip.open(filename, 'rt')
23 elif filename.endswith('.bz2'):
24 f = bz2.open(filename, 'rt')
25 else:
26 f = open(filename, 'rt')
27 yield f
28 f.close()
29
30def gen_concatenate(iterators):
31 '''

Callers 1

example.pyFile · 0.85

Calls 2

openMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected