Python創(chuàng)建系統(tǒng)目錄的方法
更新時間:2015年03月11日 09:43:23 作者:泥人張
這篇文章主要介紹了Python創(chuàng)建系統(tǒng)目錄的方法,實例分析了Python操作目錄的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了Python創(chuàng)建系統(tǒng)目錄的方法。分享給大家供大家參考。具體如下:
Python2 mkdir在沒有上級目錄時創(chuàng)建會失敗.該方法可以創(chuàng)建多級目錄。
/temp/gapgers/upload/images/1.png
如過temp文件夾不存在,會創(chuàng)建空的文件夾/temp/gapgers/upload/images/以及空文件1.png。
該方法只做拋磚引玉,大神勿噴
復制代碼 代碼如下:
import os
def mkfilePower(path):
'''create dirs if the path contain a file create a empty file
if the dir's file is exist return False else return True
ex:path = r'c:/temp/gapgers/upload/images/1.png'
nomatter there have dir temp or not,we will create it and create a empty file 1.png
'''
paths = path.split('/')
temppath = ''
for index,_spilt in enumerate(paths):
if index == 0:
temppath = _spilt
continue
temppath = temppath + '/' + _spilt
if os.path.isdir(temppath):
pass
elif index == len(paths)-1:
if os.path.isfile(temppath):
return False
fl = open(temppath,'w')
fl.close()
else:
os.mkdir(temppath)
return True
def mkfilePower(path):
'''create dirs if the path contain a file create a empty file
if the dir's file is exist return False else return True
ex:path = r'c:/temp/gapgers/upload/images/1.png'
nomatter there have dir temp or not,we will create it and create a empty file 1.png
'''
paths = path.split('/')
temppath = ''
for index,_spilt in enumerate(paths):
if index == 0:
temppath = _spilt
continue
temppath = temppath + '/' + _spilt
if os.path.isdir(temppath):
pass
elif index == len(paths)-1:
if os.path.isfile(temppath):
return False
fl = open(temppath,'w')
fl.close()
else:
os.mkdir(temppath)
return True
希望本文所述對大家的Python程序設計有所幫助。
您可能感興趣的文章:
相關文章
Python?time時間格式化和設置時區(qū)實現(xiàn)代碼詳解
這篇文章主要介紹了Python?time時間格式化和設置時區(qū)實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值2023-02-02
使用Python的Tornado框架實現(xiàn)一個Web端圖書展示頁面
Tornado是Python的一款高人氣Web開發(fā)框架,這里我們來展示使用Python的Tornado框架實現(xiàn)一個Web端圖書展示頁面的實例,通過該實例可以清楚地學習到Tornado的模板使用及整個Web程序的執(zhí)行流程.2016-07-07
利用python在excel里面直接使用sql函數(shù)的方法
今天小編就為大家分享一篇利用python在excel里面直接使用sql函數(shù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02

