欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

python對文件目錄的操作方法實例總結

系統 1934 0

本文實例講述了python對文件目錄的操作方法。分享給大家供大家參考,具體如下:

python 可以很方便的對文件進行打開,讀寫操作,刪除操作,也可以很方便的對文件夾進行遍歷操作。總體說來,有如下幾個方面:

1. python 遍歷文件目錄,當然可以遞歸
2. python 刪除文件
3. python 對文件進行重命名操作
4. python 創建文件夾 (多個層級創建)
5. python 刪除文件夾? (多個層級刪除)
6. python 移動文件
7. python 查找文件
8. 得到文件夾的大小

下面的代碼是我在用python 做一個網盤服務端的時候用到的一些方法,記錄下來,以供以后參考.

            
#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')


          

上面的代碼,根據方法的命名,就可以知道 python 操作文件以及文件夾的各種方法。

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python文本文件操作技巧匯總》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》及《Python入門與進階經典教程》

希望本文所述對大家Python程序設計有所幫助。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 精品国产三级 | 日韩免费视频播播 | 日日骚| 国产成人无码区免费内射一片色欲 | 青青青国产依人精品视频 | 男女下面一进一出无遮挡着 | 亚洲欧美综合精品久久成人 | 成人亚洲一区二区色情无码潘金莲 | 日韩欧美精品 | 欧美日本一区视频免费 | 国产a区| 男女性关系视频免费观看软件 | 欧洲精品在线视频 | 性xxxx免费观看视频 | 国产亚洲欧美日韩v在线 | 爱爱无遮挡| 91精品国产一区二区三区 | 国产欧美一区二区精品忘忧草 | 夜夜爽夜夜叫夜夜高潮漏水 | 奇米影视555| 青草娱乐| 国产成人综合AV在线观看不止 | 日本高清色本在线www | 国产精品久久久久不卡 | 国产精品国产精品 | 国产精品成人不卡在线观看 | 狠狠五月 | 国产va免费精品观看精品 | 国产精品久久久久久久久久iiiii | 奇米线在人线免费视频 | 国产精品一区二区三区免费 | 五月婷婷社区 | 香蕉久久夜色精品国产小优 | 一级片视频免费 | 久www| 国产精品揄拍100视频最近 | 亚洲欧美日韩精品一区 | 日本三级网址 | 欧美成人午夜免费完成 | 龙珠z国语版在线观看 | 日韩精品网址 |