Loop keeps freezing midway through the code

Viewed 42

I'm trying to create a code that cleans up a file I have so I can use it as data, but every time I run it, it freezes mid way, at "173529" every single time. The only reason I can think of is that I'm overworking my laptop and the console can't catch up or something. I can't see any reason or error that would cause such an issue, so I'm hoping somebody can identify it for me...

(Before all this is just a bunch of variables that edit the code and clean it up a bit before the main filtering process. The print codes are just there for debugging purposes. 'cn' is character number value, and 'nil' is number in list value)

import time
import re
import is
os.system("color")

 def spamp2():
    userList = []
    countertls = 0
    counterpus = 0
    a = open(document, "r+", encoding='utf8').read().replace("[breaker]", "")
    b = a.replace(" ","\n")
    c = b.replace(":heart:", "")
    d = c.replace(r":\w+:", "")
    e = d.replace(":", "\n")
    f = e.replace("  ", " ")
    g = f.replace("  ", " ")
    h = g.replace(" ", "")
    i = h.replace("", " ")
    j = i.replace("", "")
    k = j.replace(":", "")
    l = k.splitlines()
    m = [*set(l)]
    print("Running...")
    numinList = len(m)
    time.sleep(5)
    while numinList != 0:
        for i in m:
            if "" in i:
                ##  Character Number Value
                cn = len(i)
                ##  Number In List Value
                nil = len(m)
                if cn > 22:
                    m.remove(i)
                    ##countertls+1
                    print("\x1B[1mStringChar22+ Removed\x1B[0m")
                    print("CN Value = "+str(cn))
                    print("NuminList Value = "+str(nil)+"\n")
                    
                else:
                    userList.append(i)
                    m.remove(i)
                    ##counterpus+1
                    print("\x1B[1mPotentialUsername Added\x1B[0m")
                    print("CN Value = "+str(cn))
                    print("NuminList Value = "+str(nil)+"\n")

    else:
        print("Printing...")
        time.sleep(8)
        with open("tempy.txt", "a+", encoding='utf8') as tmp:
            tmp.truncate()
            tmp.write(userList)
            ##print("\nCharTLS Removed: "+countertls)
            ##print("CounterPUS Added: "+counterpus)
            print("\n   Proccess Completed  ")

Here's a sample of the data (Changed the usernames to conceal identity. It's just a big text file of a Discord log without any new lines):

Username: the problem with that was that it flew into the street    Nameuser: that's rought dude    Username: thankfully it didn't cause an accident otherwise it wouldn't be as fun [breaker]  but hooray for coke fireworks [breaker]  :partying_face:    

If anything else needs to be provided, please let me know.

2 Answers

It seems like you're not recalculating numinList inside the while loop so it never decreases. Therefore, you have an infinite loop.

Also it would be helpful to know more about what the data looks like.

You seem to be making this way more complicated than necessary.

m = [name for name in m if "" in name]
userList = [name for name in m if len(name) > 22]
print("Printing...")
os.sleep(8)
with open("tempy.txt", "w", encoding='utf8') as tmp:
    tmp.write("\n".join(userList) + "\n")

Use w mode when opening the file to truncate automatically before writing. And the argument to tmp.write() has to be a string, not a list.

Related