Friday 6 October 2023

Read CSVs files Get the No of cols,rows & column Name of Each CSV files using List , Pandas and OS Libraries of Python

 #!/usr/bin/env python

import pandas as pd

import os


####Read the files  the one by one from the folder and store the names of file in the list

source_folder=r"D:\\Rawdata\\Pythod_Code_RawData\\"

File_name_list=[]


for file in os.listdir(source_folder):

    if  file.endswith(".csv"):

        File_name_list.append(file)

        


print("Total File in the List",len(File_name_list))        

    


####Read the files names from the list and make df of them and get no of the columns and rows of the df and column name of df

## and store them in the list name mylist.


mylist=[]


try:

    for i in range(0,len(File_name_list)):

           dest_file=source_folder+File_name_list[i]

           df1=pd.read_csv(dest_file,on_bad_lines='skip')

           dfcols=len(df1.axes[1])

           dfrows=len(df1.axes[0])

           mylist.append(File_name_list[i]) 

           mylist.append(dfcols)

           mylist.append(dfrows)

           mylist.append(list(df1.columns))

                   

except pd.errors.EmptyDataError:

       print('Empty csv file!')


#print the mylist list and convert the mylist in dataframe by passing  the name of the list in DataFrame object and write df to csv

for i in range(0,len(mylist)):

    print(mylist[i])


new_df = pd.DataFrame(mylist)

new_df.to_csv(".//Result//2.csv", index=False, encoding='utf-8-sig')