Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试os.mkdir时出现Python操作错误_Python_Python 3.x - Fatal编程技术网

尝试os.mkdir时出现Python操作错误

尝试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

我的代码片段。返回此处显示的最后一行的OSError。为什么?和第五行做的完全一样,但第五行就像一个符咒。 错误:

类似的: 然而,他对我有不同的错误。有人告诉我为什么会这样吗?

试试:

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