本文實(shí)例講述了python對(duì)文件目錄的操作方法。分享給大家供大家參考,具體如下:
python 可以很方便的對(duì)文件進(jìn)行打開,讀寫操作,刪除操作,也可以很方便的對(duì)文件夾進(jìn)行遍歷操作。總體說來,有如下幾個(gè)方面:
1. python 遍歷文件目錄,當(dāng)然可以遞歸
2. python 刪除文件
3. python 對(duì)文件進(jìn)行重命名操作
4. python 創(chuàng)建文件夾 (多個(gè)層級(jí)創(chuàng)建)
5. python 刪除文件夾 (多個(gè)層級(jí)刪除)
6. python 移動(dòng)文件
7. python 查找文件
8. 得到文件夾的大小
下面的代碼是我在用python 做一個(gè)網(wǎng)盤服務(wù)端的時(shí)候用到的一些方法,記錄下來,以供以后參考.
#coding:utf-8 import StringIO import json import os import time import glob import shutil DATETIMEFORMATER='%Y-%m-%d %X' #only for windows RECYCLED_FOLDER_NAME='Recycled' def dateformat(datetime): '''return GMT TIME,need to change to LOCAL TIME''' return time.strftime( DATETIMEFORMATER,time.gmtime(datetime) ) def filesizeformat(size): ''' Convert file size to string ''' KBSIZE=1024.00 strSize='0 Byte' if (size < KBSIZE): strSize = '%.2f Byte' % (size) elif (size >= KBSIZE and size < KBSIZE**2): strSize = '%.2f K' % (size / KBSIZE) elif (size >= KBSIZE**2 and size < KBSIZE**3): strSize = '%.2f M' % (size / KBSIZE / KBSIZE) elif (size >= KBSIZE**3): strSize = '%.2f G' % (size / KBSIZE / KBSIZE / KBSIZE) return strSize def listdir(path): if os.path.isfile(path): return '[]' allFiles=os.listdir(path) retlist=[] for cfile in allFiles: fileinfo={} filepath=(path+os.path.sep+cfile).replace("\\","/") if cfile==RECYCLED_FOLDER_NAME: continue if os.path.isdir(filepath): fileinfo['isfile'] = '0' fileinfo['size'] = getfoldersize(filepath) else: fileinfo['isfile'] = '1' fileinfo['size'] = os.path.getsize(filepath) fileinfo['name'] = cfile fileinfo['lastvisittime'] = dateformat( os.path.getatime(filepath) ) fileinfo['createtime'] = dateformat( os.path.getctime(filepath) ) fileinfo['lastmodifytime'] = dateformat( os.path.getmtime(filepath) ) retlist.append(fileinfo) retStr=json.dumps(retlist,encoding='utf-8') return retStr def deletefile(path): if os.path.exists(path): os.remove(path) def rename(old,new): if os.path.exists(old): os.rename(old, new) def checkoutfile(path): pass def checkinfile(path): pass def lockfile(path): pass def unlockfile(path): pass def createfolder(path): if not os.path.exists(path): os.mkdir(path) def createfolders(path): if not os.path.exists(path): os.makedirs(path); def deletefolder(path): if os.path.isdir(path): os.rmdir(path) def retreeExceptionHandler(fun,path,excinfo): pass def deletefolders(path): # if os.path.isdir(path): # os.removedirs(path) shutil.rmtree(path,ignore_errors=False,onerror=retreeExceptionHandler) def movefile(old,new): shutil.move(old, new) def getfoldersize(path): size = 0 for root, dirs, files in os.walk(path): size += sum([os.path.getsize(os.path.join(root, name)) for name in files]) return size def searchfile(path,ext): returnList=glob.glob1(path, ext) return returnList if __name__=='__main__': listdir('c:/vDriver') #searchfile('c:/vDriver','*.log')
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
標(biāo)題名稱:python對(duì)文件目錄的操作方法實(shí)例總結(jié)-創(chuàng)新互聯(lián)
文章起源:http://vcdvsql.cn/article14/ddgige.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、動(dòng)態(tài)網(wǎng)站、域名注冊(cè)、網(wǎng)站改版、云服務(wù)器、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容