python中如何在写文件之前删除文件内容_python脚本删除n天之前的文件

管理Linux经常用到python脚本,然后写了脚本后,经常为了生成的文件占用磁盘空间而犯愁,这些写个函数以方便以后使用:def rmdaybefore(pfile, days):

"""

Delete pfile diectory days before files below

:param pfile: local path

:param days: before days

:return: a list os.listdir pfile

"""

d = 0

try:

d = int(days)

except ValueError,e:

print "You input the parameters of the days cannot be converted to int."

sys.exit(1)

BEDAYS = time.time() - (24 * 60 * 60 * d)

if os.path.isdir(pfile):

for f in os.listdir(pfile):

fname = pfile + os.sep + f

if os.path.isfile(fname):

fmtime = os.path.getmtime(fname)

if fmtime <= BEDAYS:

os.remove(fname)

return os.listdir(pfile)

else:

"You input the parameters of the pfile is not a directory."

sys.exit(1)

if __name__ == '__main__':

pfile = r"C:\\Users\\XXX\\Desktop\\html\\"

for f in rmdaybefore(pfile,0.2):

print f

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值