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



  • Hi,
    I have a List like this :

    myList = [
     "LOT_1,T49359/22",
     "LOT_2,T49360/22",
     "LOT_3,T49361/22",
     "LOT_4,T49362/22",
     "LOT_5,T49363/22",
     "LOT_6,T49364/22",
     "LOT_7,T49365/22",
     "LOT_8,T49366/22",
     "LOT_9,T49367/22",
     "LOT_10,T49368/22",
     "LOT_11,T49369/22",
     "LOT_12,T49370/22",
     "LOT_13,T49371/22",
     "LOT_14,T49372/22",
     "LOT_15,T49373/22",
           "..."
     "LOT_995,T50125/22"]
    

    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")
    print('Finished')
    
    

    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.



  • @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

    {'LOT_1':'T49359/22'
     ...
    }
    

    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


 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.