0

Im trying to make it so it will check from a list in a directory if every file name from the list exists or not if yes skip and if no create it but i keep runing in to the eror SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape here is a code to just print the name of every file in a directory that works on the same princeble. (if i do only c: for the directory it prints every file in the code directory)

import os


list_of_files = os.listdir('FILE_DIRECTORY_HERE')

print(list_of_files)
  • 1
    You need to replace ```\``` to ```\\``` or ```/``` –  Jun 02 '21 at 06:25
  • i tried running the following code using python3 and it worked great: `import os list_of_files = os.listdir('/') print(list_of_files)` – Cool Breeze Jun 02 '21 at 06:26
  • I didn't get it as there is no need to check if a file exists or not because `os.listdir('PATH')` returns files that exists only so there is no need to check and to fix that error just replace `C:\Users` with `C:\\Users` or before the string put `r` to declare it as a raw string `r'C:\Users\'` or just use simple slashes `'C:/Users/'` – random_hooman Jun 02 '21 at 06:35

2 Answers2

0

Use like this -

    import os
    list_of_files = os.listdir('C:\\temp')
    print(list_of_files)
Majid Hajibaba
  • 3,105
  • 6
  • 23
  • 55
0

The solution is replacing the \ with /