Aug-03-2020, 12:00 AM
I want to output a CSV file with the filepath and file content in the directory. I tried the following code, but it only iterates through files in the folder. I want it to iterate through the entire directory, including subdirectories:
import csv from pathlib import Path with open('big.csv', 'w', encoding='Latin-1') as out_file: csv_out = csv.writer(out_file) csv_out.writerow(['FileName', 'Content']) for fileName in Path('.').resolve().glob('*.txt'): lines = [ ] with open(str(fileName.absolute()),'rb') as one_text: for line in one_text.readlines(): lines.append(line.decode(encoding='Latin-1',errors='ignore').strip()) csv_out.writerow([str(fileName),' '.join(lines)])