python mkdir用子文件夹制作文件夹? [重复]

时间:2021-11-06 00:29:11

This question already has an answer here:

这个问题在这里已有答案:

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()函数,它可以创建中间目录。