Click here to Skip to main content
15,914,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to match the filename by passing a list of substrings to avoid some files in loop if the string matches with the filename. Here's my code:
list_of_files = glob.glob('D:/folder/*.gz') # create the list of file
mylist=['abcd11','abcd12','jklm12','wxyz.12']
for file_name in list_of_files:
   for ind in df.index:
        if df['filename'][ind] in file_name: #filename column contains full file names 
            for i in mylist:
                if i not in file_name:
                    print(file_name)

The thing is, it is also returning filenames which are matching with those list items. following code is working if I pass only one substring in for loop, but I want to match multiple substrings to avoid multiple files.
list_of_files = glob.glob('D:/folder/*.gz') # create the list of file
for file_name in list_of_files:
   for ind in df.index:
        if df['filename'][ind] in file_name: #filename column contains full file names 
            if 'abcd11' not in file_name:
                    print(file_name)


What I have tried:

I tried with two way
###########################first
list_of_files = glob.glob('D:/folder/*.gz') # create the list of file
mylist=['abcd11','abcd12','jklm12','wxyz.12']
for file_name in list_of_files:
   for ind in df.index:
        if df['filename'][ind] in file_name: #filename column contains full file names 
            for i in mylist:
                if i not in file_name:
                    print(file_name)

################second
list_of_files = glob.glob('D:/folder/*.gz') # create the list of file
mylist=['abcd11','abcd12','jklm12','wxyz.12']
for file_name in list_of_files:
   for ind in df.index:
        if df['filename'][ind] in file_name: #filename column contains full file names 
            is_in_list = False
            for i in mylist:
                if i in file_name:
                    is_in_list = True
                if not is_in_list:
                    print(file_name)
Posted
Comments
Richard MacCutchan 1-Nov-21 4:40am    
What is df and what does it contain?

You could combine the two tests with an or operator.
rebel0 1-Nov-21 5:06am    
Hi @Richard MacCutchan, df is name of my pandas dataframe which contains filenames and other related attributes. I am matching df['filename'] with file_name because i only want to process files which are in dataframe
Richard MacCutchan 1-Nov-21 5:42am    
So exactly what are you trying to get from the code? A list of files that are in the dataframe and do not contain any of the strings in the list? Or something different?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900