欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

python 可以很方便的對(duì)文件進(jìn)行打開,讀寫操作,刪除操作,也可以很方便的對(duì)文件夾進(jìn)行遍歷操作??傮w說(shuō)來(lái),有如下幾個(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í)候用到的一些方法,記錄下來(lái),以供以后參考.

#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 操作文件以及文件夾的各種方法。

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

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • python+pytest接口自動(dòng)化之日志管理模塊loguru簡(jiǎn)介

    python+pytest接口自動(dòng)化之日志管理模塊loguru簡(jiǎn)介

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

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

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

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

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

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

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

    Python中g(shù)etpass模塊無(wú)回顯輸入源碼解析

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

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

    簽到,都是規(guī)律性的操作,何嘗不寫一個(gè)程序加到Windows實(shí)現(xiàn)自動(dòng)簽到呢,本文就主要介紹了如何利用Python實(shí)現(xiàn)自動(dòng)打卡簽到的實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下
    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正則匹配及頁(yè)面抓取的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • Python實(shí)現(xiàn)8種常用抽樣方法

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

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

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

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

最新評(píng)論