检查并创建目录mkdir
os.listdir()
os.path.isdir()
os.path.join()
os.mkdir()
1 # -*- coding:utf-8 -*- 2 import os,sys 3 4 5 def mkdir(path = os.getcwd()): 6 if 'img' in os.listdir(path) and os.path.isdir(os.path.join(path,'img')): 7 pass 8 else: 9 os.mkdir(os.path.join(path,'img')) 10 subpath = os.path.join(path,'img') 11 dirs = ['t31','t32','t33','t34','t35','t36','t37','t38','t39'] 12 for dir in dirs: 13 if dir in os.listdir(subpath) and os.path.isdir(os.path.join(subpath,dir)): 14 pass 15 else: 16 os.mkdir(os.path.join(subpath,dir)) 17 18 if __name__=='__main__': 19 mkdir('C:\\For Work')