Find all CSV files in a directory using Python

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

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

11条回答
  •  佛祖请我去吃肉
    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
    

提交回复
热议问题