I have a list of CSV files (all have the same fields), some of which are empty. My goal is to find the first nonempty file and use that as the "base" file for an aggregation. After finding the base file, the way I will consume the rest of the files changes, so I need to maintain the index of the file. Here is what I currently have:
def f(list_of_files): aggregated_file = "" nonempty_index = -1 for index, file in enumerate(list_of_files): aggregated_file = extract_header_and_body(file) if aggregated_file: nonempty_index = index break if nonempty_index != -1: rest_of_files = list_of_files[nonempty_index:] for file in rest_of_files: aggregated_file += extract_body(file) return aggregated_file
extract_body return string representations (with and without column names, respectively) of the CSV files -- if the file is empty, the empty string is returned. This seems like a clunky solution, especially for Python. Is there a more concise/readable way to accomplish this?