import osdef mkdir_p(filename):
try:folder=os.path.dirname(filename)if not os.path.exists(folder):os.makedirs(folder)return Trueexcept:return False
用法:
filename = "./download/80c16ee665c8/upload/backup/mysql/2014-12-22/adclient_sql_2014-12-22-13-38.sql.gz"
if (mkdir_p(filename):print "Created dir :%s" % (os.path.dirname(filename))
import osfrom os.path import join as join_paths
def mk_dir_recursive(dir_path):
if os.path.isdir(dir_path):returnh, t = os.path.split(dir_path) # head/tailif not os.path.isdir(h):mk_dir_recursive(h)
new_path = join_paths(h, t)if not os.path.isdir(new_path):os.mkdir(new_path)