最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實現(xiàn)的文件夾清理程序分享

 更新時間:2014年11月22日 15:30:25   投稿:junjie  
這篇文章主要介紹了python實現(xiàn)的文件夾清理程序分享,可以按時間清理和指定配置文件清理,需要的朋友可以參考下

使用:

復(fù)制代碼 代碼如下:

foldercleanup.py -d 10 -k c:\test\keepfile.txt c:\test

表示對c:\test目錄只保留最近10天的子文件夾和keepfile.txt中指定的子文件夾。

代碼:

復(fù)制代碼 代碼如下:

import os
import os.path
import datetime
 
def getOption():
  from optparse import OptionParser
 
  des   = "clean up the folder with some options"
  prog  = "foldercleanup"
  ver   = "%prog 0.0.1"
  usage = "%prog [options] foldername"
 
  p = OptionParser(description=des, prog=prog, version=ver, usage=usage,add_help_option=True)
  p.add_option('-d','--days',action='store',type='string',dest='days',help="keep the subfolders which are created in recent %days% days")
  p.add_option('-k','--keepfile',action='store',type='string',dest='keepfile',help="keep the subfolders which are recorded in text file %keepfile% ")
  options, arguments = p.parse_args()
 
  if len(arguments) != 1:
    print("error: must input one directory as only one parameter ")
    return
 
  return options.days, options.keepfile, arguments[0] 

 
def preCheckDir(dir):
  if(not os.path.exists(dir)):
    print("error: the directory your input is not existed")
    return
  if(not os.path.isdir(dir)):
    print ("error: the parameter your input is not a directory")
    return
   
  return os.path.abspath(dir)
 
def isKeepByDay(dir, day):
  indays = False
  if( day is not None) :
    t = os.path.getctime(dir)
    today = datetime.date.today()
    createdate = datetime.date.fromtimestamp(t)
    indate = today - datetime.timedelta(days = int(day))
    print (createdate)
    if(createdate >= indate):
      indays = True
  print (indays)
  return indays
 
def isKeepByKeepfile(dir, keepfile):
  needkeep = False
  print (dir)
  if (keepfile is not None):
    try :
      kf = open(keepfile,"r")
      for f in kf.readlines():
        print (f)
        if (dir.upper().endswith("\\" + f.strip().upper())):
          needkeep = True
      kf.close()
    except:
      print ("error: keep file cannot be opened")
  print(needkeep)
  return needkeep
   
def removeSubFolders(dir, day, keepfile):
  subdirs = os.listdir(dir)
  for subdir in subdirs:
    subdir = os.path.join(dir,subdir)
    if ( not os.path.isdir(subdir)):
      continue
    print("----------------------")
    if( (not isKeepByDay(subdir, day))and (not isKeepByKeepfile(subdir, keepfile))):
      print("remove subfolder: " + subdir)
      import shutil
      shutil.rmtree(subdir,True)
   
def FolderCleanUp():
  (day, keepfile, dir) = getOption()
  dir = preCheckDir(dir)
  if dir is None:
    return
  removeSubFolders(dir,day,keepfile)
 
if __name__=='__main__':
  FolderCleanUp()

對目錄下保留最后的zip文件:

復(fù)制代碼 代碼如下:

def KeepLastNumZips(num)
    def extractTime(f):
        return os.path.getctime(f)

    zipfiles = [os.path.join(zipdir, f)
                for f in os.listdir(zipdir)
                if os.path.splitext(f)[1] == ".zip"]
    if len(zipfiles) > num:
        zipfiles.sort(key=extractTime, reverse=True)
        for i in range(num, len(zipfiles)):
            os.remove(zipfiles[i])

相關(guān)文章

  • Keras深度學(xué)習(xí)模型Sequential和Model詳解

    Keras深度學(xué)習(xí)模型Sequential和Model詳解

    這篇文章主要介紹了Keras深度學(xué)習(xí)模型Sequential和Model詳解,在Keras中有兩種深度學(xué)習(xí)的模型:序列模型(Sequential)和通用模型(Model),差異在于不同的拓?fù)浣Y(jié)構(gòu),,需要的朋友可以參考下
    2023-08-08
  • python實現(xiàn)用類讀取文件數(shù)據(jù)并計算矩形面積

    python實現(xiàn)用類讀取文件數(shù)據(jù)并計算矩形面積

    今天小編就為大家分享一篇python實現(xiàn)用類讀取文件數(shù)據(jù)并計算矩形面積,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • Python繪制移動均線方法 含源代碼

    Python繪制移動均線方法 含源代碼

    上一篇文章我們介紹了Python繪制專業(yè)的K線圖,講解了數(shù)據(jù)獲取、K線圖繪制及成交量繪制等內(nèi)容。本篇將在上一篇的基礎(chǔ)上,繼續(xù)講解移動均線的繪制,需要的朋友可以參考下
    2021-10-10
  • python繪制散點圖詳細(xì)步驟(從0到1必會)

    python繪制散點圖詳細(xì)步驟(從0到1必會)

    這篇文章主要介紹了如何使用Python繪制散點圖,包括導(dǎo)入包、準(zhǔn)備數(shù)據(jù)、繪制圖像、修飾圖像(添加標(biāo)題、坐標(biāo)軸標(biāo)簽、顏色圖例)以及整合所有代碼,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-12-12
  • python3中datetime庫,time庫以及pandas中的時間函數(shù)區(qū)別與詳解

    python3中datetime庫,time庫以及pandas中的時間函數(shù)區(qū)別與詳解

    這篇文章主要介紹了python3中datetime庫,time庫以及pandas中的時間函數(shù)區(qū)別與詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • 對python3.4 字符串轉(zhuǎn)16進(jìn)制的實例詳解

    對python3.4 字符串轉(zhuǎn)16進(jìn)制的實例詳解

    今天小編就為大家分享一篇對python3.4 字符串轉(zhuǎn)16進(jìn)制的實例詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • Win8.1下安裝Python3.6提示0x80240017錯誤的解決方法

    Win8.1下安裝Python3.6提示0x80240017錯誤的解決方法

    這篇文章主要為大家詳細(xì)介紹了Win8.1下安裝Python3.6提示0x80240017錯誤的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • python繪制已知點的坐標(biāo)的直線實例

    python繪制已知點的坐標(biāo)的直線實例

    今天小編就為大家分享一篇python繪制已知點的坐標(biāo)的直線實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • python3.3使用tkinter開發(fā)猜數(shù)字游戲示例

    python3.3使用tkinter開發(fā)猜數(shù)字游戲示例

    這篇文章主要介紹了python3.3使用tkinter開發(fā)猜數(shù)字游戲示例,需要的朋友可以參考下
    2014-03-03
  • python面積圖之曲線圖的填充

    python面積圖之曲線圖的填充

    這篇文章主要介紹了python面積圖之曲線圖的填充,文章圍繞主題的相關(guān)資料展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下,希望對你的學(xué)習(xí)有所幫助
    2022-06-06

最新評論

临泽县| 紫金县| 上蔡县| 尚义县| 奉新县| 班玛县| 丰台区| 怀柔区| 大化| 南皮县| 奉贤区| 望江县| 安塞县| 墨竹工卡县| 博乐市| 巴里| 三门县| 苏州市| 浙江省| 清水河县| 玉山县| 涞水县| 普格县| 大连市| 晋城| 元阳县| 太湖县| 和硕县| 淳安县| 察雅县| 扬州市| 孟州市| 鄂温| 武宣县| 衡水市| 汪清县| 溆浦县| 祥云县| 伊金霍洛旗| 马公市| 资溪县|