Fix the header scan over a folder listing
Your task
Your ETL job reads the header line of every CSV file in a folder. The first version opened each file and never closed it, so it ran out of file handles after a few thousand files. It also crashed when the folder held a subfolder. The new version works from the folder listing, and opens and closes one file at a time.
An assistant wrote read_headers_safely(names, first_lines, is_file). It is in the editor. It has a bug. Find it and fix it.
Input and output:
names: the entry names from the folder listing, in listing order.first_lines: the first line of each entry, at the same position.is_file: true when the entry at that position is a regular file, false when it is a folder.- Return a
name=headerstring for each entry kept, in listing order.
Rules the tests check:
- If the three lists differ in length, use only the positions all three have.
- Skip an entry that is not a file.
- Skip a hidden entry: one whose name starts with
.. - The header is the first line with every character whose code point is 32 or below removed from both ends. That covers space, tab, newline, carriage return and the other control characters. Nothing else is trimmed.
- Skip an entry whose header is empty after trimming.
- Keep names exactly as given. The same name can appear twice, and then both are kept.
Examples
read_headers_safely(names: list[str], first_lines: list[str], is_file: list[bool]) -> list[str]Example 1
Input
read_headers_safely(["a.csv", "b.csv"], ["id,name ", " x,y\n"], [True, True])Output
["a.csv=id,name", "b.csv=x,y"]Example 2
Input
read_headers_safely(["data.csv", "logs", ".hidden.csv"], ["id,ts", "", "id,ts"], [True, False, True])Output
["data.csv=id,ts"]Example 3
Input
read_headers_safely(["a.csv", "b.csv"], ["", "h1"], [True, True])Output
["b.csv=h1"]