Python Forum
Looping through csv files in a folder
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping through csv files in a folder
#1
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?
Reply
#2
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:
        ...
Reply
#3
Why not just glob.

from glob import glob

for entry in glob('results/*.csv'):
    with open(entry, 'r') as f:
        # action
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 466 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Rename files in a folder named using windows explorer hitoxman 3 693 Aug-02-2023, 04:08 PM
Last Post: deanhystad
  Rename all files in a folder hitoxman 9 1,387 Jun-30-2023, 12:19 AM
Last Post: Pedroski55
  How to loop through all excel files and sheets in folder jadelola 1 4,334 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  python gzip all files from a folder mg24 3 3,816 Oct-28-2022, 03:59 PM
Last Post: mg24
  delete all files and subdirectory from a main folder mg24 7 1,530 Oct-28-2022, 07:55 AM
Last Post: ibreeden
  Merge all json files in folder after filtering deneme2 10 2,256 Sep-18-2022, 10:32 AM
Last Post: deneme2
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,392 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  pyspark creating temp files in /tmp folder aliyesami 1 4,822 Oct-16-2021, 05:15 PM
Last Post: aliyesami
  Move file from one folder to another folder with timestamp added end of file shantanu97 0 2,439 Mar-22-2021, 10:59 AM
Last Post: shantanu97

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020