pythoncsvfileordereddict

Python - Read files from folder and Write CSV file in format


import glob
import os
import csv
from collections import OrderedDict

#Remove output file if already exists. Resolve the append Issue
file_path = 'C:\\Users\\Desktop\\Cobol\\Outputs\\LOC3X.csv'
if os.path.isfile(file_path):
    os.remove(file_path)
#

list_of_files = glob.glob('C:\\Users\\Desktop\\Cobol\\*.CBL')      # Input files in Folder
Fields = ['Program Name', 'LinesofCode']                   # to be displayed in output CSV file

# opening output csv file to write (Fields)
file_path = 'C:\\Users\\Desktop\\Cobol\\Outputs\\LOC3X.csv'           
with open(file_path, 'a') as csvfile1:
    csvwriter = csv.writer(csvfile1)
    csvwriter.writerow(Fields)
    csvfile1.close()

def process_files_loc(list_of_files):
    for fileName in list_of_files:
        with open(fileName) as i:
            count = sum(1 for line in i)
            
            my_dict = {i : count}        #input filename and its lines of code
            ordered_dict = OrderedDict()               #using OrderedDict
            print(ordered_dict)

            # creating ordered dict from dict
            ordered_dict = OrderedDict(my_dict)
            print(ordered_dict)

            # writing records of Program name and LinesofCode to output csv file
            file_path = 'C:\\Users\\Desktop\\Cobol\\Outputs\\LOC3X.csv'
            with open(file_path, 'a') as csvfile2:
                csvwriter = csv.writer(csvfile2)
                csvwriter.writerows(ordered_dict)
                csvfile2.close()
                        
process_files_loc(list_of_files)

Output in Teminal (Error):

PS C:\Users\Python-1> & C:/Users/AppData/Local/Programs/Python/Python310/python.exe c:/Users/Python-1/one.py
OrderedDict()
OrderedDict([(<_io.TextIOWrapper name='C:\\Users\\Desktop\\Cobol\\ABCDEFGH.CBL' mode='r' encoding='cp1252'>, 191)])
OrderedDict()
OrderedDict([(<_io.TextIOWrapper name='C:\\Users\\Desktop\\Cobol\\IJKLMNOP.CBL' mode='r' encoding='cp1252'>, 195)])

Actual output of file in Folder:

C:\Users\Desktop\Cobol\Outputs

Name        Date Modified       Type                Size
LOC3X.csv   9/15/2022 time      Comma Seperated     1KB

Problem: Script executed and Read 2 CBL files in the Folder, and created 1 CSV file in output folder. The output CSV file to have,

Program Name    LinesofCode
ABCDEFGH.CBL    191
IJKLMNOP.CBL    195

However, the actual output lines in CSV file is,

Program Name    LinesofCode

Solution

  • Try something like this:

    import glob
    import csv
    import os
    
    
    def process_files_loc(files):
        res = []
        for file in files:
            with open(file) as f:
                line_count = len([line.strip("\n") for line in f if line != "\n"])
            res.append([os.path.basename(f.name), line_count])
        return res
    
    
    if __name__ == '__main__':
        with open('C:\\Users\\Main\\Desktop\\test\\test.csv', 'w', newline='') as f:
            csvwriter = csv.writer(f)
            csvwriter.writerow(['Program Name', 'LinesofCode'])
            csvwriter.writerows(process_files_loc(glob.glob('C:\\Users\\Main\\Desktop\\test\\*.PY')))
    

    Result:

    result

    Regards,