python删除N天前的文件

2020年9月8日 07:01 by wst

python运维

起因

随着时间的增加,日志文件越来越多,所占空间也越来越大。

如果使用shell命令进行删除,又不熟悉,只好采用python操作。

不过想想也没啥,都是脚本语言嘛,python操作也不算笨!

方法

运行时有两个参数:path--要删除的目录,days--删除多少天前的文件。如:

deepinwst@Dell:~$ python d.py tjava 3
删除文件 tjava/StudyEnglish.java 成功
删除文件 tjava/StudyEnglish.class 成功
删除文件 tjava/HelloWorld.java 成功
删除文件 tjava/HelloWorld.class 成功

还可以设置不删除的文件名,在代码中设置EXT的值即可。

具体代码(d.py)如下:

#!/usr/bin/env python
# -*-coding=utf8 -*-
import time
import os
import sys

# 设置不过滤的文件列表
EXT = {"supervisord.pid", "supervisor.sock", "cron.log"}


def deletefile(path, n):
    for eachfile in os.listdir(path):
        filename = os.path.join(path, eachfile)
        if os.path.isfile(filename):
            if eachfile in EXT:
                print(("File can not be delete:{}".format(eachfile)))
                continue
            lastmodifytime = os.stat(filename).st_mtime
            # 设置删除多久之前的文件
            endfiletime = time.time() - 3600 * 24 * n
            if endfiletime > lastmodifytime:
                os.remove(filename)
                print("删除文件 %s 成功" % filename)
        # 如果是目录则递归调用当前函数
        elif os.path.isdir(filename):
            deletefile(filename, n)


if __name__ == '__main__':
    usage = "Usage:删除某个目录path下days天前的文件:\n python d.py path days"
    if len(sys.argv) < 3:
        print(usage)
        exit(0)
    path = sys.argv[1]
    N = int(sys.argv[2])
    deletefile(path, N)

如有问题,请留言!


Comments(129) Add Your Comment

Charlesnag
“I haven’t seen you in these parts,” the barkeep said, sidling over and above to where I sat. “Name’s Bao.” He stated it exuberantly, as if say of his exploits were shared by way of settlers about multitudinous a fire in Aeternum. He waved to a expressionless hogshead hard by us, and I returned his gesture with a nod. He filled a telescope and slid it to me across the stained red wood of the bar before continuing. “As a betting fellow, I’d be willing to wager a fair portion of coin you’re in Ebonscale Reach in search more than the drink and sights,” he said, eyes glancing from the sword sheathed on my cool to the bend slung across my back. https://www.google.co.zw/url?q=https://renewworld.ru/data-vyhoda-new-world/
Kevingaw
http://team75motorsport.de/wp-content/pages/gibt_es_ein_kasino_in_polen__erlaubte_kasinos_und_wettanbieter_in_polen.html https://www.hille-eventservice.de/news/wettannahmestellen_in_polen.html http://www.google.com.sa/url?q=https://1xbitapk.com/ja/ https://www.google.co.ma/url?q=http://1xbitapp.org/index.html https://maps.google.com.cu/url?q=https://1xbitapk.com/pl/
Uiyfrq
atorvastatin 80mg drug <a href="https://lipiws.top/">purchase atorvastatin sale</a> oral atorvastatin 20mg
Lrslpx
buy ciprofloxacin for sale - <a href="https://cipropro.top/">buy cipro 500mg pill</a> buy amoxiclav pill
Xtcfhz
order ciprofloxacin pills - <a href="https://metroagyl.top/azithromycin/">augmentin over the counter</a> buy cheap generic clavulanate
Wwjegn
ciprofloxacin order - <a href="https://septrim.top/erythromycin/">buy erythromycin generic</a> erythromycin 500mg oral
Qbcydr
metronidazole 200mg pills - <a href="https://metroagyl.top/keflexcapsule/">buy amoxil</a> azithromycin 250mg tablet
Aslhad
ivermectin without prescription - <a href="https://keflexin.top/ceftin/">buy ceftin tablets</a> tetracycline 250mg without prescription
Qfxrxf
valacyclovir oral - <a href="https://gnantiviralp.com/diltiazem/">diltiazem without prescription</a> order acyclovir 400mg
Djzghr
buy acillin online <a href="https://ampiacil.top/penicillinused/">penicillin for sale</a> amoxicillin order
Snzjdz
cost metronidazole - <a href="https://metroagyl.top/oxytetracycline/">buy oxytetracycline without prescription</a> buy generic azithromycin
Duayxc
buy lasix 40mg pills - <a href="https://antipathogenc.com/prograf/">prograf price</a> order captopril generic
Elqbhx
order glucophage 1000mg without prescription - <a href="https://gnantiacidity.com/">cost glucophage 1000mg</a> order lincocin 500 mg pills
Okqnwo
retrovir canada - <a href="https://canadiangnp.com/avapro/">buy generic irbesartan 150mg</a> cheap allopurinol 300mg
Mjeutl
oral clozapine - <a href="https://genonlinep.com/accupril/">how to get accupril without a prescription</a> famotidine 40mg us
Meofpy
buy quetiapine pills for sale - <a href="https://gnkantdepres.com/bupron/">buy bupron SR without a prescription</a> buy eskalith cheap
Xqaljc
clomipramine cheap - <a href="https://antdeponline.com/sinequan/">doxepin 75mg tablet</a> cost sinequan
Ivpezk
buy atarax - <a href="https://antdepls.com/lexapro/">lexapro oral</a> purchase endep for sale
Wurdkq
order augmentin 625mg without prescription - <a href="https://atbioinfo.com/septra/">sulfamethoxazole pills</a> ciprofloxacin 1000mg oral
Lydlyv
buy cheap amoxicillin - <a href="https://atbioxotc.com/">amoxicillin online buy</a> cipro 500mg us
Cfqgdx
where to buy azithromycin without a prescription - <a href="https://gncatbp.com/">order generic azithromycin</a> ciprofloxacin 500 mg drug
Exwsez
cleocin brand - <a href="https://cadbiot.com/cefpodoxime/">generic cefpodoxime 200mg</a> chloramphenicol price
Uipdvp
stromectol price - <a href="https://antibpl.com/tylquin/">buy generic levofloxacin over the counter</a> buy cefaclor cheap
Jkvvfo
order ventolin inhaler - <a href="https://antxallergic.com/xsseroflo/">seroflo cost</a> buy theo-24 Cr 400 mg
Rcwlya
medrol tablet - <a href="https://ntallegpl.com/fluorometholone/">purchase fml-forte without prescription</a> buy astelin paypal
Vriqbe
order desloratadine 5mg pills - <a href="https://rxtallerg.com/flixotidespray/">buy cheap flixotide</a> ventolin 2mg inhaler
Anozir
glyburide 5mg usa - <a href="https://prodeprpl.com/pioglitazone30/">where can i buy actos</a> dapagliflozin 10 mg usa
Ykjwcb
cost glucophage 1000mg - <a href="https://arxdepress.com/losartan25/">order losartan sale</a> acarbose order online
Vdaozz
repaglinide 2mg over the counter - <a href="https://depressinfo.com/jardiance25/">jardiance 10mg uk</a> buy empagliflozin no prescription
Mgsuea
buy generic semaglutide 14mg - <a href="https://infodeppl.com/glucovance5/">glucovance ca</a> desmopressin buy online
Wimcgc
how to get terbinafine without a prescription - <a href="https://treatfungusx.com/adiflucanv/">fluconazole pill</a> griseofulvin online buy
Ifoiaq
buy ketoconazole no prescription - <a href="https://antifungusrp.com/">order nizoral 200mg</a> sporanox us
Qkerkx
buy famciclovir 250mg - <a href="https://amvinherpes.com/acyclovircream/">purchase zovirax online cheap</a> valcivir brand
Caekwl
order digoxin pill - <a href="https://blpressureok.com/furosemidediuret/">order furosemide for sale</a> buy lasix pill diuretic
Gdiqzi
generic metoprolol 100mg - <a href="https://bloodpresspl.com/telmisartan/">telmisartan pill</a> order adalat 30mg without prescription
Xqxhox
hydrochlorothiazide 25mg cost - <a href="https://norvapril.com/bisoprolol/">zebeta 5mg drug</a> buy generic zebeta 5mg
Qnmqwf
order nitroglycerin generic - <a href="https://nitroproxl.com/clonidinechlorthalidone/">combipres medication</a> valsartan generic
Kgvaxk
simvastatin mortal - <a href="https://canescholest.com/">simvastatin nearby</a> lipitor whose
Mdcasf
rosuvastatin pills knee - <a href="https://antcholesterol.com/ezetimibe/">ezetimibe careless</a> caduet pills danger
Gdajbo
buy viagra professional element - <a href="https://edsildps.com/">viagra professional online minute</a> levitra oral jelly online insult
Sgumil
priligy further - <a href="https://promedprili.com/sildigra/">sildigra perfect</a> cialis with dapoxetine slight
Gxyrkl
cenforce online gleam - <a href="https://xcenforcem.com/kamagrasildenafil/">kamagra pills live</a> brand viagra palm
Xwsusi
brand cialis musical - <a href="https://probrandtad.com/tadorapills/">tadora undoubted</a> penisole dance
Nfmiyk
brand cialis subtle - <a href="https://probrandtad.com/apcalissx/">apcalis despite</a> penisole experience
Tihltj
cialis soft tabs value - <a href="https://supervalip.com/cialissuperactive/">cialis super active pills punish</a>1 viagra oral jelly dream
Ankknr
cialis soft tabs pills affection - <a href="https://supervalip.com/viagrasuperactive/">viagra super active pills flutter</a> viagra oral jelly online instinct
Wpuamt
dapoxetine leave - <a href="https://promedprili.com/sildigra/">sildigra involve</a> cialis with dapoxetine frame
Nsiaom
cenforce recollect - <a href="https://xcenforcem.com/">cenforce online extreme</a> brand viagra online thomas
Izpafr
acne treatment singular - <a href="https://placnemedx.com/">acne medication robot</a> acne treatment authority
Ucaeyj
asthma treatment ease - <a href="https://bsasthmaps.com/">asthma medication jack</a> asthma medication grief
Iagmkq
uti medication problem - <a href="https://amenahealthp.com/">uti treatment element</a> uti medication physical
Vnkhuw
prostatitis treatment permit - <a href="https://xprosttreat.com/">prostatitis pills steward</a> pills for treat prostatitis profession
Rlmowh
valtrex pills noise - <a href="https://gnantiviralp.com/">valacyclovir online jump</a> valtrex pills collar
Uujlkf
loratadine associate - <a href="https://clatadine.top/">loratadine medication foul</a> claritin pills hat
Fqlzcb
dapoxetine onto - <a href="https://prilixgn.top/">dapoxetine admire</a> dapoxetine twin
Fveafj
claritin scar - <a href="https://clatadine.top/">claritin pills departure</a> claritin pills savage
Wzddls
ascorbic acid armor - <a href="https://ascxacid.com/">ascorbic acid upper</a> ascorbic acid snake
Hoodld
promethazine prepare - <a href="https://prohnrg.com/">promethazine honour</a> promethazine younger
Xcxfpf
clarithromycin pills wood - <a href="https://gastropls.com/gncytotec/">cytotec pills mount</a> cytotec wife
Lgvzju
fludrocortisone fourth - <a href="https://gastroplusp.com/prlansoprazole/">lansoprazole activity</a> lansoprazole over
Bjdyfl
buy rabeprazole 20mg - <a href="https://gastrointesl.com/domperidone/">buy domperidone online</a> domperidone generic
Yfoujs
where to buy dulcolax without a prescription - <a href="https://gastroinfop.com/liv52/">order generic liv52 10mg</a> liv52 20mg uk
Mtbvfd
buy bactrim generic - <a href="https://tobmycin.com/">buy tobramycin drops</a> cost tobrex 10mg
Yawsnv
buy generic eukroma - <a href="https://danaterone.shop/">buy zovirax cream for sale</a> order duphaston online
Fanwmf
order fulvicin for sale - <a href="https://dipyrilx.com/">order dipyridamole generic</a> order gemfibrozil
Sjzsfz
dapagliflozin pills - <a href="https://forpaglif.com/">dapagliflozin 10mg without prescription</a> acarbose over the counter
Rqldgz
order dramamine 50 mg for sale - <a href="https://bisacolax.com/">dimenhydrinate 50 mg cheap</a> cheap risedronate 35mg
Nnyvgp
vasotec 10mg us - <a href="https://vasolapril.com/">cheap vasotec 5mg</a> purchase latanoprost for sale
Kpokrv
etodolac 600 mg canada - <a href="https://etodograph.com/">etodolac 600 mg without prescription</a> pletal 100mg without prescription
Ukeoob
piroxicam pills - <a href="https://rivastilons.com/">buy generic rivastigmine 6mg</a> exelon 6mg cost
Hkbkeb
buy generic hydrea - <a href="https://hydroydrinfo.com/disulfiram/">buy generic disulfiram</a> robaxin for sale online
Vevspk
depakote 500mg drug - <a href="https://adepamox.com/topiramate/">topiramate 100mg ca</a> topamax 200mg brand
Vllzql
buy cheap norpace - <a href="https://anorpica.com/">buy norpace</a> chlorpromazine online
Bqzvyf
buy aldactone paypal - <a href="https://aldantinep.com/">purchase spironolactone for sale</a> naltrexone 50mg for sale
Qggmbw
buy cytoxan generic - <a href="https://cycloxalp.com/stavudine/">purchase stavudine generic</a> buy trimetazidine without prescription
Pepesw
purchase cyclobenzaprine sale - <a href="https://abflequine.com/enalapril/">order enalapril 10mg online</a> enalapril 10mg usa
Bsvrpl
order ondansetron 8mg generic - <a href="https://azofarininfo.com/procyclidine/">kemadrin pills</a> ropinirole 1mg usa
Gsvlkw
cheap ascorbic acid 500mg - <a href="https://mdacidinfo.com/bromhexine/">cheap generic bromhexine</a> buy compro pill
Qotsim
order durex gel for sale - <a href="https://xalaplinfo.com/durexcondoms/">durex condoms online purchase</a> buy generic xalatan
Fglzqt
rogaine over the counter - <a href="https://hairlossmedinfo.com/finasteridehl/">purchase propecia sale</a> order proscar 1mg pills
Cbucqm
cheap arava 20mg - <a href="https://infohealthybones.com/alfacalcidol/">purchase alfacalcidol generic</a> cartidin pills
Oexkvq
calan over the counter - <a href="https://infoheartdisea.com/valsartan/">diovan cheap</a> buy tenoretic tablets
Hoemsg
atenolol pill - <a href="https://heartmedinfox.com/">buy tenormin</a> buy carvedilol generic
Fhrqep
buy atorlip for sale - <a href="https://infoxheartmed.com/lisinopril/">order zestril without prescription</a> purchase nebivolol online
Dajpuk
oral gasex - <a href="https://herbalinfomez.com/">order gasex</a> buy diabecon without a prescription
Gjgcum
buy lasuna cheap - <a href="https://infoherbalmz.com/himcolin/">buy cheap generic himcolin</a> buy himcolin generic
Keofga
order norfloxacin online cheap - <a href="https://gmenshth.com/flutamide/">buy flutamide pills</a> confido online order
Scyswc
speman price - <a href="https://spmensht.com/">buy speman tablets</a> oral fincar
Bkkmdc
finax online buy - <a href="https://finmenura.com/alfuzosin/">buy uroxatral 10mg for sale</a> cheap alfuzosin
Ijtsjt
terazosin 5mg usa - <a href="https://hymenmax.com/dutasteride/">order avodart pills</a> buy priligy generic
Tdfwhs
order trileptal for sale - <a href="https://trileoxine.com/pirfenidone/">cheap pirfenex without prescription</a> buy levoxyl pills
Ydyebj
buy imusporin sale - <a href="https://asimusxate.com/gloperba/">buy generic colchicine over the counter</a> buy cheap generic colchicine
Hqtpcn
lactulose brand - <a href="https://duphalinfo.com/">oral lactulose</a> betahistine 16mg sale
Orcqqw
buy deflazacort no prescription - <a href="https://lazacort.com/brimonidine/">brimonidine ca</a> buy brimonidine generic
Osadtq
buy besivance sale - <a href="https://besifxcist.com/sildamax/">buy sildamax generic</a> order sildamax online
Lfkppy
neurontin 600mg oral - <a href="https://aneutrin.com/sulfasalazine/">azulfidine 500mg sale</a> order azulfidine 500 mg online
Qbftht
probenecid 500mg ca - <a href="https://bendoltol.com/monograph/">etodolac sale</a> buy carbamazepine 400mg pill
Yuuawx
mebeverine cost - <a href="https://coloxia.com/etoricoxib/">buy arcoxia 60mg pill</a> oral cilostazol 100mg
Msgbcd
buy celecoxib sale - <a href="https://celespas.com/flavoxate/">cheap flavoxate without prescription</a> cost indomethacin 50mg
Phvxjc
voltaren 100mg pill - <a href="https://dicloltarin.com/aspirin/">generic aspirin 75 mg</a> generic aspirin 75 mg
Stlowr
purchase rumalaya pill - <a href="https://rumaxtol.com/shallaki/">buy shallaki pills</a> endep us
Acpfyv
buy pyridostigmine 60 mg pills - <a href="https://mestonsx.com/">pyridostigmine 60mg for sale</a> azathioprine price
Rtimqr
order generic diclofenac - <a href="https://vovetosa.com/animodipine/">cheap generic nimotop</a> cheap nimodipine online
Yxvwtl
baclofen for sale online - <a href="https://baclion.com/">where to buy baclofen without a prescription</a> buy feldene sale
Fmnywv
mobic 7.5mg usa - <a href="https://meloxiptan.com/asrizatriptan/">buy generic maxalt for sale</a> ketorolac sale
Npubxz
cyproheptadine 4 mg generic - <a href="https://periheptadn.com/aszanaflex/">tizanidine online buy</a> tizanidine for sale
Nmvqui
trihexyphenidyl for sale online - <a href="https://voltapll.com.com/">purchase artane without prescription</a> order emulgel
Jxymkd
buy generic omnicef - <a href="https://omnixcin.com/clindamycin/">buy cheap clindamycin</a> buy clindamycin without a prescription
Ripdfi
buy cheap generic isotretinoin - <a href="https://aisotane.com/asdeltasone/">deltasone 10mg drug</a> deltasone 5mg oral
Apdllk
order prednisone 20mg online cheap - <a href="https://apreplson.com/permethrin/">zovirax creams</a> zovirax without prescription
Uhdtwj
buy generic acticin online - <a href="https://actizacs.com/">buy acticin online</a> order tretinoin cream
Jfvxpb
betnovate oral - <a href="https://betnoson.com/">order betamethasone</a> buy benoquin
Yoopcu
metronidazole over the counter - <a href="https://cenforcevs.com/">order cenforce 100mg online cheap</a> order cenforce 100mg without prescription
Kaqztd
augmentin 375mg brand - <a href="https://baugipro.com/">augmentin 375mg pills</a> oral levothyroxine
Cltbzn
clindamycin sale - <a href="https://indometnp.com/">indomethacin brand</a> indocin 50mg cost
Luohby
losartan 50mg brand - <a href="https://acephacrsa.com/">order cephalexin pills</a> order keflex 500mg sale
Lsyyzs
buy eurax paypal - <a href="https://aeuracream.com/caczone/">purchase aczone for sale</a> aczone us
Phwwyp
modafinil 100mg without prescription - <a href="https://sleepagil.com/">buy modafinil paypal</a> order melatonin online
Nyeasn
order bupropion 150 mg generic - <a href="https://bupropsl.com/shuddhaguggulu/">where can i buy shuddha guggulu</a> order shuddha guggulu sale
Egzzna
cost progesterone 100mg - <a href="https://apromid.com/gynelotrimin/">cheap ponstel</a> clomiphene where to buy
Jtiewo
purchase capecitabine generic - <a href="https://xelocap.com/">xeloda online</a> order generic danazol 100 mg
Fwnbib
aygestin where to buy - <a href="https://norethgep.com/bimatoprost/">order bimatoprost</a> cheap yasmin generic
Ymkegc
alendronate 70mg canada - <a href="https://pilaxmax.com/pilex/">pilex order</a> brand medroxyprogesterone 10mg
Jwaxdz
buy generic dostinex 0.25mg - <a href="https://adostilin.com/cabgolin/">how to get cabgolin without a prescription</a> alesse for sale
Isnnye
purchase estradiol online cheap - <a href="https://festrolp.com/anastrozole/">buy arimidex 1mg generic</a> where to buy arimidex without a prescription
Ofhwaq
バイアグラ 薬局で買える - <a href="https://jpedpharm.com/tadalafil/">シアリス おすすめ</a> シアリス 値段
Mwmvch
プレドニン 飲み方 - <a href="https://jpaonlinep.com/">プレドニン 海外通販</a> アジスロマイシン の購入
Pkvela
プレドニンは薬局で買える? - <a href="https://jpanfarap.com/">プレドニン錠 20 mg 強さ</a> イソトレチノイン 飲み方
Waxgda
eriacta rack - <a href="https://eriagra.com/asildigrah/">sildigra bother</a> forzest injury