I am trying to count all the As and Bs and Cs in all the .txt files I supply and make a .csv file that lists the counts one by one of all those letters.
The code here does all I want but only with the last file I supply instead of all of them.
What am I doing wrong?
import glob
import csv
#This will print out all files loaded in the same directory and print them out
for filename in glob.glob('*.txt*'):
print(filename)
#A B and C
substringA = "A"
Head1 = (open(filename, 'r').read().count(substringA))
substringB = "B"
Head2 = (open(filename, 'r').read().count(substringB))
substringC = "C"
Head3 = (open(filename, 'r').read().count(substringC))
header = ("File", "A Counts" ,"B Counts" ,"C Counts")
analyzed = (filename, Head1, Head2, Head3)
#This will write a file named Analyzed.csv
with open('Analyzed.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(header)
writer.writerow(analyzed)
ABandCin the for loop or outside of it?forloop :)