我正在运行flask项目,并且正在寻找一种方法来创建目录,该目录位于运行当前App的路径之上。例如:dirA --> dirBinA --> peter.py griffin.sh dirCinA --> index.py <--------- this is the flask app that's running tom.css dick.html harry.js dirDinA --> <--------- this directory doesn't exist yet anotherDir --> turtle.py我想从正在运行的flask应用程序anotherDir的新目录中创建一个新目录dirDinAdirCinA/index.py如果我尝试os.mkdir("../dirDinA/anotherDir/"),则烧瓶会说OSError: [Errno 2] No such file or directory: '../dirDinA/anotherDir'
3 回答

侃侃尔雅
TA贡献1801条经验 获得超16个赞
为了创建一个新的2级深度目录,您需要分两个步骤创建它。例如,如果../dirDinA尚不存在,则以下命令将失败。
os.mkdir("../dirDinA/anotherDir")
它会产生OSError: No such file or directory误导性的,向您显示您要创建的完整路径,而不是突出显示不存在会导致错误的ACTUAL部分。
但是,以下两步方法运行良好,没有任何错误
os.mkdir("../dirDinA")
os.mkdir("../dirDinA/anotherDir")
../dirDinA必须先存在目录,然后anotherDir才能在其中创建目录
添加回答
举报
0/150
提交
取消