iterating through specific files in folder with name matching pattern in python

Viewed 75

I have a folder with a lot of csv files with different names. I want to work only with the files that their name is made up of numbers only, though I have no information of the range of the numbers in the title of the files.

for example, I have ['123.csv', 'not.csv', '75839.csv', '2.csv', 'bad.csv', '23bad8.csv'] and I would like to only work with ['123.csv', '75839.csv', '2.csv']

I tried the following code:

for f in file_list:
    if f.startwith('1' or '2' or '3' ..... or '9'):
        # do something

but this does not some the problem if the file name starts with a number but still includes letters or other symbols later.

3 Answers

You can do it this way:

file_list = ["123.csv", "not.csv", "75839.csv", "2.csv", "bad.csv", "23bad8.csv"]
for f in file_list:
    name, ext = f.rsplit(".", 1)    # split at the rightmost dot
    if name.isnumeric():
        print(f)

Output is

123.csv
75839.csv
2.csv

You can use Regex to do the following:

import re
lst_of_files = ['temo1.csv', '12321.csv', '123123.csv', 'fdao123.csv', '12312asdv.csv', '123otk123.csv', '123.txt']
pattern = re.compile('^[0-9]+.csv')
newlst = [re.findall(pattern, filename) for filename in lst_of_files if len(re.findall(pattern, filename)) > 0]
print(newlst)

One of the approaches:

import re
lst_of_files = ['temo1.csv', '12321.csv', '123123.csv', 'fdao123.csv', '12312asdv.csv', '123otk123.csv', '123.txt', '876.csv']
for f in lst_of_files:
    if re.search(r'^[0-9]+.csv', f):
        print (f)

Output:

12321.csv
123123.csv
876.csv
Related