gpt4 book ai didi

python - python中的目录字符串不起作用

转载 作者:行者123 更新时间:2023-11-28 17:30:35 26 4
gpt4 key购买 nike

我想为源字符串设置一个目录路径,但我得到了一个奇怪的结果。

首先,这很好用

 source = "C:\Users\Owner\Documents\Py"
os.listdir(source)

和列表:

['python vocab - files.txt', 'test destination folder', 'test source folder']

但是如果我想在一个额外的层次上钻取,我会得到一个错误

source = "C:\Users\Owner\Documents\Py\test destination folder"
os.listdir(source)
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
mylist = os.listdir(source)
WindowsError: [Error 123] The filename, directory name or volume label syntax is incorrect: 'C:\\Users\\Owner\\Documents\\Py\test destination folder/*.*'`

最佳答案

问题是您没有转义反斜杠。在目录中,您会看到 'C:\\Users\\Owner\\Documents\\Py\test destination folder,其中 '\\' 只是一个反斜杠和'\t' 是一个制表符。
将其更正为原始字符串 r'C:\Users\Owner\Documents\Py\test destination folder\*.*' 或只是转义反斜杠 ('C:\\Users\\Owner\\Documents\\Py\\测试目标文件夹\\*.*')

关于python - python中的目录字符串不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34643542/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com