pythondirectoryfile-management

"SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape" . (File management bug)


So I'm making a program in Python that goes through all of your files in the download folder but when I run it, it says

(SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape).

I use a variable to store the directory and put it in a for loop with the OS library. please help me. (Windows 11, python 3.9.)

I know many people have asked this question and I have gone through all of the answers but none of them works for me, I think the problem I have, sounds similar to others but it actually is very different, so please don't mark this as duplicate. please help :)

Code:

#im trying to make a program that goes through all the files in my downloads folder

import os
from time import sleep


source_dir = "C:\Users\(replace with you'r name to test)\example\Downloads"

with os.scandir(source_dir) as entries:
    for entry in entries:
        print(entry.name)
        sleep(0.35)

I have tried to change the \ with / and with // and with \, but none of the different types work. i have also tried removing the " and also replacing them with ', it didnt work. please help


Solution

  • And if you want to remove desktop.ini from printing just write this code

    import os
    from time import sleep
    
    
    source_dir = "C:/Users/DAKSH/Downloads"
    
    for file in os.listdir(source_dir):
        if not (file.endswith('.ini')):
            print(file)