Search code examples
pythonglobfile-search

Most efficient way for searching all files with the same filename in python


I want to find the extensions for all files with the same name in a folder. So for example, if a directory contains 2 files named test.csv and test.txt. I want the program to return a list containing ".txt" and ".csv".

I have used:

glob.glob(file_name + '*') 

which works but is quite slow (0.03 seconds) when the folder contains many files. Is there some faster way of doing this in python?


Solution

  • try something like :

    import re
    import os
    
    files = os.listdir(".")
    
    my_files = re.findall(r"(\w+\.php)", " ".join(files))
    
    print(my_files)