I think you could try with glob.glob, what should help
import numpy as np
import glob
import cv2
import csv
Libraries ⬆️; You know what⬇️
image_list = []
for filename in glob.glob(r'C:\your path to\file*.png'): # '*' will count files each by one
#Read
img = cv2.imread(filename)
flattened = img.flatten()
print(flattened) # recommend to avoid duplicates, see files and so on.
#Save
with open('output2.csv', 'ab') as f: #ab is set
np.savetxt(f, flattened, delimiter=",")
Cheers
Also, find an easier method that is making fast and not weight image/csv
image_list = []
with open('train_train_.csv', 'w') as csv_file:
csv_writer = csv.writer(csv_file, delimiter ='-')
for filename in glob.glob(r'C:\your path to\file*.png'):
img = cv2.imread(filename)
image_list.append(img)
csv_writer.writerow(img)
print(img)