Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple matches using file's startswith

Tags:

python

regex

I have a program that takes an input a list of numbers ( comma separated ) and I am supposed to run through the the files that start with those numbers

myprogram.py 1,6,8 

have to go through files that are 1001_filename, 1004_filename, 6001_filename, 8003_filename, 8004_filename etc.,

one way is to iterate through 3 times( once for 1*, 6*, 8* ) and do if

for file_type in file_types:
    file.startswith(file_type): 

but how can I match for any in the list ?

Is there a regex that can do something like :

file.startswith(any of file_types): file_types here is 1,6,8 or something to that effect ?

like image 566
Victor Avatar asked Oct 14 '25 17:10

Victor


1 Answers

You can use glob to find all your files:

from glob import glob
path = "path_to/"
files = glob(path+"[1,5,8]*")

We will match any file starting with 1, 5 or 8 in whatever directory path points to.

like image 187
Padraic Cunningham Avatar answered Oct 17 '25 14:10

Padraic Cunningham