Find all CSV files in a directory using Python

后端 未结 11 1784
生来不讨喜
生来不讨喜 2020-12-02 17:57

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

11条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-02 18:36

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

    import os
    
    files = os.listdir("/path-to-dir")    
    files = list(filter(lambda f: f.endswith('.csv'), files))
    
    # lambda returns True if filename name ends with .csv or else False
    # and filter function uses the returned boolean value to filter .csv files from list files.
    

提交回复
热议问题