0

I'm trying to have this program use all of the CSV files inside of a folder. The folder is on my desktop, and it's called "Web_Scraper" (I'm on a mac btw).

This is how the code looks; what do I replace whatever is inside of the "enumerate" with (the 1.csv, 2.csv, 3.csv, etc)?

from openpyxl import Workbook
import csv
import os

wb = Workbook()
ws = wb.worksheets[0]

header_keys = []
for n, fName in 

enumerate(['3.csv','4.csv','5.csv','6.csv','7.csv','8.csv','9.csv','10.csv','11.csv','12.csv',]):
with open(fName) as fh:
    csv_reader = csv.DictReader(fh, fieldnames=['header', 'data'], delimiter=',')
    if n == 0:
        for values in csv_reader:

I think I'm supposed to use something like os.listdir but I'm not exaclty sure what the syntax should be.

  • What is the problem btw? Please mention it –  Aug 03 '17 at 05:02
  • Btw, you can also use glob.glob('*.csv') – user1427026 Aug 03 '17 at 05:05
  • Possible duplicate of [How do I list all files of a directory?](https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory) – user1427026 Aug 03 '17 at 05:16
  • Possible duplicate of [Find all files in a directory with extension .txt in Python](https://stackoverflow.com/questions/3964681/find-all-files-in-a-directory-with-extension-txt-in-python) – SwiftsNamesake Aug 03 '17 at 05:16

4 Answers4

1

You can take advantage of the glob module

import glob

scraper_files = glob.glob('*.csv') //returns an array of filenames
Josh Hamet
  • 957
  • 8
  • 10
1

you can use something like below,

import os
from pathlib import Path

rootdir = 'xx/xx/x'

for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        if ('.csv' in Path.suffix)
            csv_reader = csv.DictReader(file, fieldnames=['header', 'data'], delimiter=',')
Prakash Palnati
  • 3,231
  • 22
  • 35
0

You can use os module

import os
os.listdir('~/Desktop/Web_Scraper') # Returns list of files or directory  names inside that dir

And you can apply filter on file name extension to filter out only csv files

viveksyngh
  • 777
  • 7
  • 15
0

You can use python's inbuilt os module.

import os
os.listdir("this-directory")  # returns list of files in this-directory

E.g.

import os
for file in os.listdir("."):  # reading from current directory, replace it with your directory.
   if file.endswith(".csv"):
      with open(file) as fh:
          csv_reader = csv.DictReader(fh, fieldnames=['header', 'data'], delimiter=',')
              if n == 0:
                  for values in csv_reader:

Hope it helps!

Om Prakash
  • 2,675
  • 4
  • 29
  • 50