Python Forum
Problem with For "str" in str["str"]:
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with For "str" in str["str"]:
#1
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 Angry 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.
Reply
#2
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]:]:
Reply
#3
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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020