国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實現的文件夾清理程序分享

2020-02-23 06:12:37
字體:
來源:轉載
供稿:網友

使用:
代碼如下:
foldercleanup.py -d 10 -k c:/test/keepfile.txt c:/test

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

代碼:
代碼如下:
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("http://" + f.strip().upper())):
          needkeep = True
      kf.close()
    except:
      print ("error: keep file cannot be opened")
  print(needkeep)
  return needkeep

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临漳县| 鄂温| 重庆市| 渭源县| 南澳县| 凤山市| 河北省| 台州市| 会理县| 游戏| 新竹市| 论坛| 许昌市| 佛山市| 澜沧| 年辖:市辖区| 定襄县| 应用必备| 化隆| 余姚市| 汉阴县| 乐亭县| 新昌县| 崇左市| 合作市| 遂川县| 隆林| 昭平县| 都江堰市| 田阳县| 万宁市| 调兵山市| 赤峰市| 清涧县| 横峰县| 三河市| 定南县| 武宁县| 上饶市| 淮滨县| 白城市|