0

I am very new to Python, and not professional coding background.

I have 40+ csv files, with name format all start with DT7D. There is no heading on each of the csv, and all columns are lined up.

I am using below code (which I found from other Stack overflow post) to merge all the csv files and put into one consolidated csv file:

os.chdir("C:/Users/xxx/Downloads")
file_extension = '.csv'
all_filenames = [i for i in glob.glob(f"*{file_extension}")]
combined_csv_data = pd.concat([pd.read_csv(f,error_bad_lines=False, index_col=False, 
dtype='unicode',header=None ) for f in all_filenames], sort=False, axis=0)
combined_csv_data.to_csv('DT7DAll.csv', index=False)

However, now there is an empty csv and response error, I found piece of code below to skip empty csv:

if os.stat(file).st_size != 0:

But I am not sure how to put this line of code into my above existing code, so that it can skip any empty csv file and move onto next csv file.

Any help would be greatly appreciated.

Thank you

  • Use `all_filenames = [i for i in glob.glob(f"*{file_extension}") if os.stat(file).st_size != 0]` – jezrael May 31 '22 at 05:48

0 Answers0