I have a directory of CSV data files and I load all of them in one line using pandas.read_csv()
within a list comprehension statement.
import glob
import pandas as pd
file_list = glob.glob('../data/')
df_list = [pd.read_csv(f) for f in file_list]
df = pd.concat(df_list, ignore_index=True)
Now I want to print the file path every time when it loads a data file, but I cannot find a way to use multiple statements in list comprehension. For example, something like [pd.read_csv(f); print(f) for f in file_list]
will cause a SyntaxError
.
The closest thing I can get is to let print()
to return None
in an if-statement, which works like a pass
after printing.
df_list = [pd.read_csv(f) for f in file_list if print(f) is None]
Is there a proper way of doing this? I like list comprehension for its conciseness, but it does not seem to allow multiple statements.