8

Exact Match in Python Strings - codesd.com

 3 years ago
source link: https://www.codesd.com/item/exact-match-in-python-strings.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Exact Match in Python Strings

advertisements

I am trying to find a sub-string in a string, but I am not achieving the results I want.

I have several strings that contains the direction to different directories:

'/Users/mymac/Desktop/test_python/result_files_Sample_8_11/logs',
'/Users/mymac/Desktop/test_python/result_files_Sample_8_1/logs',
'/Users/mymac/Desktop/test_python/result_files_Sample_8_9/logs'

Here is the part of my code here I am trying to find the exact match to the sub-string:

 for name in sample_names:

        if (dire.find(name)!=-1): 

            for files in os.walk(dire):

                for file in files:
                    list_files=[]
                    list_files.append(file)
                    file_dict[name]=list_files

Everything works fine except that when it looks for Sample_8_1 in the string that contains the directory, the if condition also accepts the name Sample_8_11. How can I make it so that it makes an exact match to prevent from entering the same directory more than once?


You could try searching for sample_8_1/ (i.e., include the following slash). I guess given your code that would be dire.find(name+'/'). This just a quick and dirty approach.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK