os.mkdir 错误-系统找不到指定的路径 [英] os.mkdir error- system cannot find the specified path
问题描述
我正在尝试使用字典键创建新文件夹.代码是:
I am trying to create new folders using dictionary keys. The code is:
os.mkdir("Y:\\Bleeding-study\\MIMIC\\Notes\\randombins\\")
for key in index:
os.mkdir("Y:\\Bleeding-study\\MIMIC\\Notes\\randombins\\batch_%s\\"%str(key))
os.mkdir("Y:\\Bleeding-study\\MIMIC\\Notes\\randombins\\batch_%s\\%s\\"%(str(key),"config"))
os.mkdir("Y:\\Bleeding-study\\MIMIC\\Notes\\randombins\\batch_%s\\corpus\\"%str(key))
错误是:WindowsError: [Error 3] 系统找不到指定的路径:'Y:\\Bleeding-study\\MIMIC\\Notes\\randombins\\batch_0\\'
我认为这段代码会创建那个文件路径名,为什么会出现这个错误?
I thought this code would create that file pathname, so why is it giving this error?
推荐答案
您的错误是因为您试图在一个步骤中创建多个级别的目录.os.mkdir()
只会创建一个目录,所以这一行会失败:
Your error is because you are trying to create multiple levels of directory in one step. os.mkdir()
will only create a single level of directory, so this line fails:
os.mkdir("Y:\\Bleeding-study\\MIMIC\\Notes\\randombins\\batch_%s\\"%str(key))
因为您还没有创建 batch_0
目录.
os.makedirs()
是你在这里需要的:
os.makedirs()
is what you need here:
递归目录创建功能.像 mkdir(),但使所有包含叶目录所需的中级目录.
...
Recursive directory creation function. Like mkdir(), but makes all intermediate-level directories needed to contain the leaf directory.
...
...事实上,如果你使用它,那么你可以去掉初始的 os.mkdir()
,因为它是多余的.
...and in fact if you use this, then you can do away with the initial os.mkdir()
, as it will be redundant.
这篇关于os.mkdir 错误-系统找不到指定的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!