Problem with For "str" in str["str"]: - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Problem with For "str" in str["str"]: (/thread-20898.html) |
Problem with For "str" in str["str"]: - tarikjabiri - Sep-05-2019 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 thank you. RE: Problem with For "str" in str["str"]: - ThomasL - Sep-05-2019 Your condition "if result[0] in f:" might be convenient but is very inefficient and faulty as you know. Why not check directly for equality? if result[0] == f[-len(result[0]:]: RE: Problem with For "str" in str["str"]: - tarikjabiri - Sep-05-2019 Hi ThomasL thank you for your response. I forgot to mention some folders contains like "C:/subfolder/subfolder1/LOT_10 BIS" or something else this wouldn't work if I use : if result[0] == f[-len(result[0]:]:I am always looking for LOT_10 no matter what after it or before it. thank you. |