Choose a file starting with a given string

import os
prefixed = [filename for filename in os.listdir('.') if filename.startswith("prefix")]

Try using os.listdir,os.path.join and os.path.isfile.
In long form (with for loops),

import os
path = 'C:/'
files = []
for i in os.listdir(path):
    if os.path.isfile(os.path.join(path,i)) and '001_MN_DX' in i:
        files.append(i)

Code, with list-comprehensions is

import os
path = 'C:/'
files = [i for i in os.listdir(path) if os.path.isfile(os.path.join(path,i)) and \
         '001_MN_DX' in i]

Check here for the long explanation...

Tags:

Python

File