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)
MATLAB版本
函数
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)
其他
有更多代码需求欢迎联系