read all csv files in a folder and print them to screen in python

You can use the built-in csv module and os module to achieve this. Here's an example code snippet:

main.py
import csv
import os

folder_path = "/path/to/folder/with/csv/files/"

for filename in os.listdir(folder_path):
    if filename.endswith(".csv"):
        with open(os.path.join(folder_path, filename), "r", newline="") as csvfile:
            csvreader = csv.reader(csvfile)
            print(f"File: {filename}")
            for row in csvreader:
                print(row)
            print("\n")
398 chars
14 lines

This code iterates through all the files in the directory specified by folder_path. For each file that ends with the .csv extension, it opens the file using csv.reader(). The code then prints the filename and each row in the CSV file to the screen. The newline="" argument in our open() function is used to ensure that the CSV file is read correctly.

related categories

gistlibby LogSnag