尝试os.mkdir时出现Python操作错误
我的代码片段。返回此处显示的最后一行的OSError。为什么?和第五行做的完全一样,但第五行就像一个符咒。 错误: 类似的: 然而,他对我有不同的错误。有人告诉我为什么会这样吗?试试:尝试os.mkdir时出现Python操作错误,python,python-3.x,Python,Python 3.x,我的代码片段。返回此处显示的最后一行的OSError。为什么?和第五行做的完全一样,但第五行就像一个符咒。 错误: 类似的: 然而,他对我有不同的错误。有人告诉我为什么会这样吗?试试: os.mkdir(str(directory)+'\temporary') OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'c:\\testing\tempora
os.mkdir(str(directory)+'\temporary')
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'c:\\testing\temporary'
或
关于这两个\
或r'\temporary'
,是文档,是一个好问题
另外,这也是一个很好的选择,因为它在Windows上使用
\\
,但在*nix上使用/
。例如:
os.mkdir(str(directory) + r'\temporary')
这在Windows上提供了
directory\temporary
,在*nix上提供了directory/temporary
。这是一种更清晰、更简单的方法。此外,最好使用os.path.join
来构建路径,而不仅仅是添加反斜杠(Windows)或斜杠(Unix/Linux):os.mkdir(os.path.join(directory),“temporary”)
。(记住原始字符串仍然很好,因为您需要使用re
模块来记住它们。)
os.mkdir(str(directory) + '\\temporary')
os.mkdir(str(directory) + r'\temporary')
os.mkdir(os.path.join(directory), 'temporary')