亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python對文件目錄的操作方法實例總結(jié)

 更新時間:2019年06月24日 09:54:34   作者:輕舞肥羊  
這篇文章主要介紹了python對文件目錄的操作方法,結(jié)合實例形式總結(jié)分析了Python針對文件目錄相關的遍歷、刪除、移動、查找等操作技巧,需要的朋友可以參考下

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

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

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

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

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

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

更多關于Python相關內(nèi)容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python文本文件操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進階經(jīng)典教程

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

相關文章

  • python+pytest接口自動化之日志管理模塊loguru簡介

    python+pytest接口自動化之日志管理模塊loguru簡介

    python中有一個用起來非常簡便的第三方日志管理模塊--loguru,不僅可以避免logging的繁瑣配置,而且可以很簡單地避免在logging中多進程多線程記錄日志時出現(xiàn)的問題,甚至還可以自定義控制臺輸出的日志顏色,接下來我們來學習怎么使用loguru模塊進行日志管理
    2022-05-05
  • Python實戰(zhàn)之生成有關聯(lián)單選問卷

    Python實戰(zhàn)之生成有關聯(lián)單選問卷

    這篇文章主要為大家分享了一個Python實戰(zhàn)小案例——生成有關聯(lián)單選問卷,并且能根據(jù)問卷總分數(shù)生成對應判斷文案結(jié)果,感興趣的可以了解一下
    2023-04-04
  • Python如何將CSV文件轉(zhuǎn)JSON文件

    Python如何將CSV文件轉(zhuǎn)JSON文件

    這篇文章主要給大家介紹了關于Python如何將CSV文件轉(zhuǎn)JSON文件的相關資料,可以使用Python內(nèi)置的csv和json模塊來實現(xiàn)將csv文件轉(zhuǎn)為json的操作,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2023-11-11
  • pytorch之深度神經(jīng)網(wǎng)絡概念全面整理

    pytorch之深度神經(jīng)網(wǎng)絡概念全面整理

    這篇文章主要介紹了pytorch之深度神經(jīng)網(wǎng)絡概念,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • Python中getpass模塊無回顯輸入源碼解析

    Python中getpass模塊無回顯輸入源碼解析

    這篇文章主要介紹了Python中getpass模塊無回顯輸入源碼解析,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • 如何利用Python實現(xiàn)自動打卡簽到的實踐

    如何利用Python實現(xiàn)自動打卡簽到的實踐

    簽到,都是規(guī)律性的操作,何嘗不寫一個程序加到Windows實現(xiàn)自動簽到呢,本文就主要介紹了如何利用Python實現(xiàn)自動打卡簽到的實踐,具有一定的參考價值,感興趣的可以了解一下
    2021-12-12
  • Python中functools模塊函數(shù)解析

    Python中functools模塊函數(shù)解析

    這篇文章主要介紹了Python中functools模塊的常用函數(shù)解析,分別講解了functools.cmp_to_key,functools.total_ordering,functools.reduce,functools.partial,functools.update_wrapper和functools.wraps的用法,需要的朋友可以參考下
    2017-03-03
  • python采集百度百科的方法

    python采集百度百科的方法

    這篇文章主要介紹了python采集百度百科的方法,涉及Python正則匹配及頁面抓取的相關技巧,需要的朋友可以參考下
    2015-06-06
  • Python實現(xiàn)8種常用抽樣方法

    Python實現(xiàn)8種常用抽樣方法

    抽樣是統(tǒng)計學、機器學習中非常重要,本文就用Python實現(xiàn)抽樣方法,主要介紹了八種方法,感興趣的小伙伴們可以參考一下
    2021-06-06
  • Python apscheduler實現(xiàn)定時任務的方法詳解

    Python apscheduler實現(xiàn)定時任務的方法詳解

    apscheduler(Advanced Python Scheduler)是一個用于Python的靈活、強大的定時任務調(diào)度庫,它允許您以各種方式安排函數(shù)或方法的執(zhí)行,下面就跟隨小編一起學習一下它的具體使用吧
    2023-10-10

最新評論