Find all CSV files in a directory using Python

136,040

Solution 1

import os
import glob

path = 'c:\\'
extension = 'csv'
os.chdir(path)
result = glob.glob('*.{}'.format(extension))
print(result)

Solution 2

from os import listdir

def find_csv_filenames( path_to_dir, suffix=".csv" ):
    filenames = listdir(path_to_dir)
    return [ filename for filename in filenames if filename.endswith( suffix ) ]

The function find_csv_filenames() returns a list of filenames as strings, that reside in the directory path_to_dir with the given suffix (by default, ".csv").

Addendum

How to print the filenames:

filenames = find_csv_filenames("my/directory")
for name in filenames:
  print name

Solution 3

By using the combination of filters and lambda, you can easily filter out csv files in given folder.

import os

all_files = os.listdir("/path-to-dir")    
csv_files = list(filter(lambda f: f.endswith('.csv'), all_files))

# lambda returns True if filename (within `all_files`) ends with .csv or else False
# and filter function uses the returned boolean value to filter .csv files from list files.

Solution 4

use Python OS module to find csv file in a directory.

the simple example is here :

import os

# This is the path where you want to search
path = r'd:'

# this is the extension you want to detect
extension = '.csv'

for root, dirs_list, files_list in os.walk(path):
    for file_name in files_list:
        if os.path.splitext(file_name)[-1] == extension:
            file_name_path = os.path.join(root, file_name)
            print file_name
            print file_name_path   # This is the full path of the filter file

Solution 5

I had to get csv files that were in subdirectories, therefore, using the response from tchlpr I modified it to work best for my use case:

import os
import glob

os.chdir( '/path/to/main/dir' )
result = glob.glob( '*/**.csv' )
print( result )
Share:
136,040
mintgreen
Author by

mintgreen

Updated on July 05, 2022

Comments

  • mintgreen
    mintgreen almost 2 years

    How can I find all files in directory with the extension .csv in python?

  • mintgreen
    mintgreen about 12 years
    i'm having a problem with what im doing with this code im trying to display all the content in th directory using, csv = csv.reader(open(filenames, 'rb')) and its giving me an error" coercing to unicode: need string or buffer"can you help me out here please thanks alot if you can i'll apreciate it.
  • ppasler
    ppasler over 7 years
    This a short solution, but note, that this only scans in the current directory (where your script is running). To change that use os.chdir("/mydir"), as provided here: stackoverflow.com/questions/3964681/…
  • thclpr
    thclpr over 7 years
    @ppasler Hi, Answer edited with your sugestion. Also i think now it's more pythonic :)
  • Shashank Jain
    Shashank Jain over 5 years
    This will help to identify path also of these csv files.
  • Suraj Rao
    Suraj Rao over 5 years
    Use edit to add information to your answer instead of adding as a comment please. Also use Ctrl + K to format code
  • Kingsley
    Kingsley over 5 years
    Please explain how this code solves the OP's question.
  • Nav
    Nav over 2 years
    Isn't there a way to do this without changing the directory? Can't we specify the directory as part of the glob command itself?