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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With