Python Forum

Full Version: Looping through csv files in a folder
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

Got a major question. How can we set up a loop to go through every csv file in a folder and have ourprogram to run for each one?

Here is what we have right now however it's not working:

from os import listdir
from typing import List

files = listdir("results")
allLines = []

for i in files:
    files = open("results/" + i, "r")
    contents = files.readlines()
    allLines.append(contents)
print(allLines)

for xFile in allLines:
Any suggestions?
I haven't tested this, but looks ok if you have errors, let me know.
Note pathlib requires python 3.6 or newer
from pathlib import Path

directory = '/home/me/Downloads' # put your directory here
filepath = Path(directory)
filenames = [fname for fname in filepath.iterdir() if fname.is_file() and fname.suffix == '.csv']
for filename in filenames:
    with filename.open() as fp:
        ...
Why not just glob.

from glob import glob

for entry in glob('results/*.csv'):
    with open(entry, 'r') as f:
        # action
glob will work fine in this application.
Pathlib, using file objects offers things not found with glob.
for example:
list(filename.parents)
or
if filename.exists():
or for directory paths
dirname.mkdir(exist_ok=True)
which will create a directory if it doesn't already exist.