Find all CSV files in a directory using Python

2020-02-17 06:12发布

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

7条回答
可以哭但决不认输i
2楼-- · 2020-02-17 07:05

This solution uses the python function filter. This function creates a list of elements for which a function returns true. In this case, the anonymous function used is partial matching '.csv' on every element of the directory files list obtained with os.listdir('the path i want to look in')

import os

filepath= 'filepath_to_my_CSVs'  # for example: './my_data/'

list(filter(lambda x: '.csv' in x, os.listdir('filepath_to_my_CSVs')))
查看更多
登录 后发表回答