pythonvariable-assignmentfile-sortingcode-freeze

why does my code break when the extension variable is one


i was working on code for a file sorter adn came to the issue that when ever the extension variable is only 1 the code will freeze i tryed to get basic response like print("hall") but this didnt work after cheking the len of the extensio i was even more confused cause it clearly displayed 1 so yer pls help

pls see above for what i tryed and i was expacting to for now skip that extesion and just go on with the other files in a folder

import os
foldername=input("name of folder: ")


path=os.listdir(foldername)

with open("C:/Users/flori/OneDrive/Dokumente/code/extesions.txt","r") as f:
   
        k=f.read()
        

n=0
while len(path)!=n:
    #getting the extension from the file to sort it in the folders
    extension=path[n].split(".")
    print(len(extension))
    print(extension)
    #looking if the extesion is matching with any registerd extesion in the file "k"
    
    
    
    
    ü=0
    b=0
    with open("C:/Users/flori/OneDrive/Dokumente/code/extesions.txt","r") as f:
                
                 k=f.readlines()  

        
    while b < len(k):
          
         if len(extension)==2:
            
            for i in k:
                print(len(k)-1)
                






               
                
                print(len(extension))

                if extension[1]==k[b][:-1]:
                    print("match")
                    


                print(k[b])
                b=b+1
         
                
    
    
    
    
    
            ü=ü+1
         else:
             path[+1]
    n=n+1
    "/n"
    "/n"
    "/n"

Solution

  • In the section path[+1], it seems to be attempting to increment the index of the path, but you are not updating n, hence you are stuck in an infinite loop. The correct way to increase the loop counter would be n += 1.

    The strings "/n" will not print a newline. To print newline, you should use print("\n").

    You are reading the extensions file twice. Once to read the entire content and then to read line by line. This is redundant.

    It's more Pythonic to use a for loop to iterate through each file in path rather than a while loop.

    I've tried to address these issues as following:

    
    import ios
    
    foldername = input("name of folder: ")
    path = os.listdir(foldername)
    
    # Just read the extensions once and store them in a list
    with open("C:/Users/flori/OneDrive/Dokumente/code/extesions.txt", "r") as f:
        extensions_list = [line.strip() for line in f.readlines()]
    
    for filename in path:
        # Getting the extension from the file to sort it in the folders
        extension = filename.split(".")
        print(len(extension))
        print(extension)
    
        # If file has no extension or is a hidden file (starts with dot)
        if len(extension) < 2:
            print("Skipping file with no extension or hidden file.")
            print("\n")
            continue
    
        for known_extension in extensions_list:
            if extension[1] == known_extension:
                print("match")
                print(known_extension)
                break
        else:
            print("No match found for this extension.")
        
        print("\n")
    

    Hope it helps !