2

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?

aminography
  • 21,986
  • 13
  • 70
  • 74
MrHat
  • 23
  • 8

2 Answers2

1

try something like :

import re
import os

files = os.listdir(".")

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

print(my_files)
LinPy
  • 16,987
  • 4
  • 43
  • 57
  • Does this not assume I know the extensions in advance? – MrHat Nov 06 '19 at 12:06
  • just an example you may use r"(MY_NAME\.\w+)" dpends on what you want to match – LinPy Nov 06 '19 at 12:08
  • Yup that worked. I have no idea how you knew that searching the regex strings would be faster but it did indeed make it doubly fast. Thanks! – MrHat Nov 06 '19 at 12:45
0

You can use a dictionary with file names as keys and extensions as values as follow:

import os
data = {}
for file in os.listdir():
    name, ext = file.split('.')
    if name not in data.keys():
        data[name ] = list()

    temp = data[name].copy()
    temp.append(ext)
    data[name] = temp

print(data)
Ahmed Hawary
  • 461
  • 4
  • 15