【Python/MATLAB】自动新建多级文件夹(上级目录可不存在) |
您所在的位置:网站首页 › matlab怎么新建一个文件夹 › 【Python/MATLAB】自动新建多级文件夹(上级目录可不存在) |
【Python/MATLAB】自动新建多级文件夹(上级目录可不存在)
Python版本MATLAB版本其他
by 今天不飞了 昨天代码报错,新建文件夹失败!一查看,欧,是因为上级路径都不存在…… 用MATLAB和Python自带的mkdir来创建文件夹,只能创建一级目录,如果上级不存在,就会失败。 所以决定写一个无视目录,无脑新建的函数。 (不排除MATLAB和Python本来就有这样的函数,因为才疏学浅没有发现……如果大佬知道,欢迎告诉我) Python版本函数 import os def mkdirs(thePath): if not os.path.exists(thePath): supPath,_ = os.path.split(thePath) if not os.path.exists(supPath): mkdirs(supPath) os.mkdir(thePath) return 0测试 testPath = r'C:\Users\Administrator\Desktop\test\lets\do\it' mkdirs(testPath)函数 function mkdirs(thePath) if ~exist(thePath,'dir') [supPath,~] = fileparts(thePath); if ~exist(supPath,'dir') mkdirs(supPath) end mkdir(thePath) end end测试 testPath = 'C:\Users\Administrator\Desktop\test\just\do\it'; mkdirs(testPath)有更多代码需求欢迎联系 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |