Find a phrase you remember, even when you’ve forgotten which note contains it.… · @chu_zheng74230 · TwKit
@chu_zheng74230
Find a phrase you remember, even when you’ve forgotten which note contains it. This AI-written Python searches UTF-8 text files in a folder named notes, including subfolders. Change repair café to your phrase. It searches for literal text, not a regular expression: from pathlib import Path from os.path import islink query = 'repair café'.casefold() for path in sorted(Path('notes').rglob('*.txt')): if islink(path) or not getattr(path, 'is_file')(): continue with open(path, encoding='utf-8') as source: for number, line in enumerate(source, 1): if query in line.casefold(): print(f'{path}:{number}: {line.rstrip()}') Actual result from three fictional notes: notes/archive/may.txt:2: REPAIR CAFÉ: check the opening time. notes/weekend.txt:2: Ask about the Repair Café at the library. The number after each filename is the line to open. Both capitalizations matched; the unrelated café note stayed out. The script only reads files and matches within a single line.
曝光 8 · 评论 0 · 点赞 0 · 书签 0
查看原帖