【问题标题】:"SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape" . (File management bug)\"SyntaxError: (unicode error) \'unicodeescape\' codec can\'t decode bytes in position 2-3: truncated \\UXXXXXXXX escape\" 。 (文件管理错误)
【发布时间】:2022-11-03 01:19:44
【问题描述】:

所以我正在用 Python 编写一个程序,它会遍历下载文件夹中的所有文件,但是当我运行它时,它说

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

我使用一个变量来存储目录并将其与 OS 库一起放入 for 循环中。请帮我。 (Windows 11,python 3.9。)

我知道很多人都问过这个问题,我已经回答了所有的答案,但没有一个对我有用作为重复。请帮忙 :)

代码:

#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)

我尝试用 / 和 // 和 \ 更改 \,但没有任何不同的类型起作用。我也试过删除“并用'替换它们,它没有用。请帮忙

【问题讨论】:

    标签: python directory file-management


    【解决方案1】:
    #You can also use this to scan you directory and get all filename in it
    
    import os
    
    
    
    source_dir = "C:/Users/(replace with you'r name to test)/example/Downloads"
    
    for file in os.listdir(source_dir):
        print(file)
    

    【讨论】:

      猜你喜欢
      • 2022-11-10
      • 2020-11-25
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 2016-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多