Python 读取文件夹路径的内容
有人能解释一下如何在windows上使用python处理路径吗 路径以参数形式给出Python 读取文件夹路径的内容,python,windows,Python,Windows,有人能解释一下如何在windows上使用python处理路径吗 路径以参数形式给出 path = 'C:\Data\Projects\IHateWindows\DEV_Main\Product\ACSF\Dev\DEV\force.com\src\aura' 我正在尝试获取文件夹的内容,但不是有效路径 正在将路径读取为: 'C:\\Data\\Projects\\IHateWindows\\DEV_Main\\Product\\ACSF\\Dev\\DEV\x0corce.com\\src\x
path = 'C:\Data\Projects\IHateWindows\DEV_Main\Product\ACSF\Dev\DEV\force.com\src\aura'
我正在尝试获取文件夹的内容,但不是有效路径
正在将路径读取为:
'C:\\Data\\Projects\\IHateWindows\\DEV_Main\\Product\\ACSF\\Dev\\DEV\x0corce.com\\src\x07ura'
尝试一些解决方案
for f in listdir(path.replace("\\", "\\\\")): print (f)
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:\\\\Data\\\\Projects\\\\Prd_Development\\\\DEV_Main\\\\Product\\\\ACSF\\\\Dev\\\\DEV\x0corce.com\\\\src\x07ura'
for f in listdir(path.replace("\\", "/")): print (f)
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:/Data/Projects/Prd_Development/DEV_Main/Product/ACSF/Dev/DEV\x0corce.com/src\x07ura'
编辑:
解决方案
path = path.replace("\a", "\\a").replace("\f", "\\f")
在Python中,单个反斜杠是转义字符。因此,您可能需要使用双反斜杠或正斜杠:
path = 'C:\\Data\\Projects\\IHateWindows\\DEV_Main\\Product\\ACSF\\Dev\\DEV\\force.com\\src\\aura'
或
尝试使用\\或/when将值赋给
path
。注意:您的编辑显示您正在试图修改Python中的路径。这就是你错的地方。您必须正确输入它——使用双反斜杠或单正斜杠。试图在Python中使用replace
“为时已晚”。(因此,它毕竟是一个副本,那里的答案应该对您有进一步的帮助。)正如我所说的,路径s作为参数接收,它是如何来的…@usr2564301您可以在Pytohn中修改路径。唯一的问题是,你需要清楚地知道你在开始时有什么,不要弄乱str表示法。@Mathieu:不,只要路径输入了错误的字符串表示法——单反斜杠而不是双反斜杠——你就无法知道原始字符串是什么。例如,您不能盲目地将每个\f
替换为\\f
。
path = 'C:/Data/Projects/IHateWindows/DEV_Main/Product/ACSF/Dev/DEV/force.com/src/aura'