检查并创建目录mkdir

时间:2021-06-06 21:33:57

os.listdir()

os.path.isdir()

os.path.join()

os.mkdir()

 # -*- coding:utf-8 -*-
import os,sys def mkdir(path = os.getcwd()):
if 'img' in os.listdir(path) and os.path.isdir(os.path.join(path,'img')):
pass
else:
os.mkdir(os.path.join(path,'img'))
subpath = os.path.join(path,'img')
dirs = ['t31','t32','t33','t34','t35','t36','t37','t38','t39']
for dir in dirs:
if dir in os.listdir(subpath) and os.path.isdir(os.path.join(subpath,dir)):
pass
else:
os.mkdir(os.path.join(subpath,dir)) if __name__=='__main__':
mkdir('C:\\For Work')