def check_and_creat_dir(file_url):
'''
判斷文件是否存在,文件路徑不存在則創建文件夾
:param file_url: 文件路徑,包含文件名
:return:
'''
file_gang_list = file_url.split('/')
if len(file_gang_list)>1:
[fname,fename] = os.path.split(file_url)
print(fname,fename)
if not os.path.exists(fname):
os.makedirs(fname)
else:
return None
#還可以直接創建空文件
else:
return None
check_and_creat_dir('file/vide/dj/data.py')
補充:os.path.split(file_url)返回的是路徑和文件名
os.path.splittext(file_url)返回的是文件地址和擴展名