Problem
There are multiple .csv files scattered across a directory and its subdirectories.
Implement process_path(path):
- recursively find all
.csvfiles - for each CSV, read each line and parse the second column as an integer
- return the total sum across all CSV files
Example:
.
|-- a
| `-- b
| `-- ex.csv
|-- example.csv
`-- ignoreme.log
ex.csv:
a,5,-1,-1,0
a,10,-1,-1,0
example.csv:
b,0,-1,-1,0
b,-3,-1,-1,0
Result: 12
Approach
Use os.walk to traverse the directory and filter by .csv extension.
Python
import os
import csv
def process_csv(filepath: str) -> int:
total = 0
with open(filepath, 'r', encoding='utf-8', errors='ignore') as f:
reader = csv.reader(f)
for row in reader:
if len(row) >= 2:
total += int(row[1])
return total
def process_path(path: str) -> int:
total = 0
for root, _, files in os.walk(path):
for name in files:
if name.endswith('.csv'):
total += process_csv(os.path.join(root, name))
return total
Complexity
- Time: proportional to total number of CSV lines read
- Space:
O(1)(streaming)
Need interview help? Contact OA VO Service
- 📧 Email: [email protected]
- 📱 Phone: +86 17863968105
Need real interview questions? Contact WeChat: Coding0201 to get the questions.