Find all CSV files in a directory using Python

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

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

相关标签:
11条回答
  • 2020-12-02 18:31
    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
    
    0 讨论(0)
  • 2020-12-02 18:31

    While solution given by thclpr works it scans only immediate files in the directory and not files in the sub directories if any. Although this is not the requirement but just in case someone wishes to scan sub directories too below is the code that uses os.walk

    import os
    from glob import glob
    PATH = "/home/someuser/projects/someproject"
    EXT = "*.csv"
    all_csv_files = [file
                     for path, subdir, files in os.walk(PATH)
                     for file in glob(os.path.join(path, EXT))]
    print(all_csv_files)
    

    Copied from this blog.

    0 讨论(0)
  • 2020-12-02 18:32

    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
    
    0 讨论(0)
  • 2020-12-02 18:32

    Use the python glob module to easily list out the files we need.

    import glob
    path_csv=glob.glob("../data/subfolrder/*.csv")
    
    0 讨论(0)
  • 2020-12-02 18:34

    Please use this tested working code. This function will return a list of all the CSV files with absolute CSV file paths in your specified path.

    import os
    from glob import glob
    
    def get_csv_files(dir_path, ext):
        os.chdir(dir_path)
        return list(map(lambda x: os.path.join(dir_path, x), glob(f'*.{ext}')))
    
    print(get_csv_files("E:\\input\\dir\\path", "csv"))
    
    0 讨论(0)
  • 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.
    
    0 讨论(0)
提交回复
热议问题