⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

Problem with For "str" in str["str"]

  • Hi,
    I have a List like this :

    myList = [

    and i have folders like this: C:/subfolder/subfolder1/LOT_830 folders from LOT_1 to LOT_995.
    My code Source is :

    for f in subfolders:
        folder_is_not_renamed = True
        for line in myList:
            result = line.replace("\n", "").replace("/", "-").split(",")
            if result[0] in f:
                myDirectory = f.replace(os.path.dirname(f), "")
                # src source
                src = f
                # dist distination
                dist = f.replace(myDirectory, '/' + result[1])
                if not os.path.exists(dist):
                    os.rename(src, dist)
                    folder_is_not_renamed = False
        if folder_is_not_renamed:
            print(f + " is not Renamed \n")

    the problem is when it arrives to folder "C:/subfolder/subfolder1/LOT_10" the "if result[0] in f" return True for LOT_1 but not the case it should return False. I know that "LOT_1" in "C:/subfolder/subfolder1/LOT_10" return True it is correct but i want a way to avoid it.

    I want to mention some folders contains like "C:/subfolder/subfolder1/LOT_10 BIS" or something else.
    I am always looking for LOT_10 no matter what after it or before it.

    thank you.

  • Banned

    @tarik-el-jabiri here is a non-coded rendition of a method to deal with this issue -- it might not be the best and then again it might just be -- it is basically I would at least initially handle this situation.

    First I would convert myList into myDict arranged as follows:

    Key = 'LOT_1'  Value = 'T49359/22'

    or in Dictionary nomenclature


    Next instead of working with the raw Folder Names run them through a preprocessor so that you can apply your business rules to that folder (aka business rules such as LOT_1 <> LOT_10, etc...)

    Then Pass the foldername into your preprocessor routine and have that routine return a Key:Value where:

    Key = 'LOT_???'   Value = 'Full_Folder_Path_Name'

    Then you can do something as easy as

    for FolderKey in ReturnedValue:
        if FolderKey in myDict.keys():
            FolderValue = ReturnedValue[FolderKey]
            Now you have the two values you are looking for already separated

    As for the Preprocessor Routine you are going to have to basically search for 'LOT_' and then because your number can be up to 3 digits grab the next 3 characters and determine what number you actually have discarding anything that is not a number. Numerous ways to do this for instance you could try to convert each character to a number and add them together such as:

    if isnumeric(String[0]):
       digit = int(String[0]
       if isnumeric(String[1]):
           digit = (digit * 10) + int(String[1]
           if isnumeric(String[2]):
               digit = (digit * 10) + int(String[2]
    NewValue = 'LOT_' + str(digit)

    I nested these because if you run into a non-integer followed by an integer you do not get the wrong end result

Log in to reply