-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
48 lines (35 loc) · 1.01 KB
/
util.py
File metadata and controls
48 lines (35 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import fileinput
"""
A text block generator
"""
def lines(file):
"""
Reads lines from the file and yields them.
:param file: The file to read from.
:return: Yields lines from the file as they are.
"""
for line in file:
yield line
yield '\n'
def blocks(file):
"""
Generates blocks of text, separated by empty lines.
:param file: The file to read from.
:return: Yields blocks of text.
"""
block = []
for line in lines(file):
if line.strip():
block.append(line)
elif block:
yield ''.join(block).strip()
block = []
'''if __name__ == '__main__':
input_file = 'test_input.txt'
block_count = 0 # Initialize the block counter
# Open the file in read mode to read the lines
with open(input_file, 'r') as input_file:
for block in blocks(input_file):
block_count += 1 # Increment the counter for each block
f.write(f"Block {block_count}:\n{block}\n")
'''