This question already has an answer here:
这个问题在这里已有答案:
- mkdir -p functionality in Python [duplicate] 12 answers
Python中的mkdir -p功能[重复] 12个答案
This works:
mkdir('folder')
but this doesn't
但事实并非如此
mkdir('folder/subfolder')
error:
WindowsError: [Error 3] The system cannot find the path specified: 'folder/subfolder'
3 个解决方案
#1
48
Try os.makedirs
instead, if you want to create a tree of directories in one call.
如果要在一次调用中创建目录树,请尝试使用os.makedirs。
#2
14
I tried the above on Linux using Python 2.6.6, but had to ensure that the string ended with a '/' (or '\', on Windows). E.g.
我在Linux上使用Python 2.6.6尝试了上述内容,但必须确保字符串以'/'(或Windows上的'\')结尾。例如。
os.makedirs('folder/subfolder/')
Otherwise only 'folder' was created.
否则只创建'文件夹'。
#3
10
I think you want the os.makedirs() function, which can create intermediate directories.
我想你想要os.makedirs()函数,它可以创建中间目录。
#1
48
Try os.makedirs
instead, if you want to create a tree of directories in one call.
如果要在一次调用中创建目录树,请尝试使用os.makedirs。
#2
14
I tried the above on Linux using Python 2.6.6, but had to ensure that the string ended with a '/' (or '\', on Windows). E.g.
我在Linux上使用Python 2.6.6尝试了上述内容,但必须确保字符串以'/'(或Windows上的'\')结尾。例如。
os.makedirs('folder/subfolder/')
Otherwise only 'folder' was created.
否则只创建'文件夹'。
#3
10
I think you want the os.makedirs() function, which can create intermediate directories.
我想你想要os.makedirs()函数,它可以创建中间目录。