[Python] 删除指定目录下后缀为 xxx 的过期文件
import os
import time
import datetime
def should_remove(path, pattern, days):
if not path.endswith(pattern):
return False
mtime = os.path.getmtime(path)
now = time.time()
result = now - mtime > days * 24 * 3600
print "\n>>>>>>>>>>>>>>\n"
print "file: ", path
print "mtime: ", datetime.datetime.fromtimestamp(mtime)
print "now: ", datetime.datetime.fromtimestamp(now)
print "> %d days: " % days, result
return result
def findNremove(path, pattern, days):
print "path: ", path
print "pattern: ", pattern
print "days: ", days
for r, d, f in os.walk(path):
for files in f:
file_path = os.path.join(r, files)
if should_remove(file_path, pattern, days):
try:
print "Removing %s" % (file_path)
#os.remove(file_path)
except Exception, e:
print e
else:
print "removed %s" % (file_path)
if __name__ == '__main__':
path = os.path.join("/home", "bshao", "projects", "driver_client", "tmp")
days = 10
pattern = ".apk"
findNremove(path, pattern, days)
[Python] 删除指定目录下后缀为 xxx 的过期文件的更多相关文章
- Python批量删除指定目录下的指定类型的文件
Python作为一种脚本语言.其很适合文件级的各种操作.以下的代码能够批量删除指定目录下的所有特定类型(CSV类型)的文件. import sys, csv , operator import os ...
- python获取指定目录下所有文件名os.walk和os.listdir
python获取指定目录下所有文件名os.walk和os.listdir 觉得有用的话,欢迎一起讨论相互学习~Follow Me os.walk 返回指定路径下所有文件和子文件夹中所有文件列表 其中文 ...
- [No000073]C#直接删除指定目录下的所有文件及文件夹(保留目录)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- linux --> 删除指定目录下所有文件
删除指定目录下所有文件 代码样例: ///////////////////////////////////////////////////// //Name: DeleteFile //Purpose ...
- python实现指定目录下批量文件的单词计数:并发版本
在 文章 <python实现指定目录下批量文件的单词计数:串行版本>中, 总体思路是: A. 一次性获取指定目录下的所有符合条件的文件 -> B. 一次性获取所有文件的所有文件行 - ...
- C# 删除指定目录下的所有文件及文件夹
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...
- Python获取指定目录下所有子目录、所有文件名
需求 给出制定目录,通过Python获取指定目录下的所有子目录,所有(子目录下)文件名: 实现 import os def file_name(file_dir): for root, dirs, f ...
- C#直接删除指定目录下的所有文件及文件夹(保留目录)
#region 直接删除指定目录下的所有文件及文件夹(保留目录) /// <summary> /// 直接删除指定目录下的所有文件及文件夹(保留目录) /// </summary&g ...
- Windows 定时删除指定路径下N天前的日志文件
Windows 定时删除指定路径下N天前的日志文件 Windows 下bat脚本文件的内容为 1. 删除指定路径下5天前的所有文件 @echo off set SrcDir=E:\WORK\Git s ...
随机推荐
- 移动端横屏(beta)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- WPF
最近在学习WPF,学习WPF首先上的是微软的MSDN,然后再搜索了一下网络有关WPF的学习资料.为了温故而知新把学习过程记录下来,以备后查.这篇主要讲WPF的开发基础,介绍了如何使用Visual St ...
- [leetcode] 题型整理之排列组合
一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...
- Codeforces Round #375 (Div. 2)
A. The New Year: Meeting Friends 水 #include <set> #include <map> #include <stack> ...
- python 自带的ide 不能保存文件
初学python 用shell写的代码结果不能保存,经查询,原因有人说是因为文件里有中文, import random secret =random.randint(1,100) guess=0 tr ...
- ACM: FZU 2110 Star - 数学几何 - 水题
FZU 2110 Star Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Pr ...
- ACM: NBUT 1105 多连块拼图 - 水题 - 模拟
NBUT 1105 多连块拼图 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format: Practice Appoint ...
- Linux下定时执行任务的几种方式
如果说我说如果,你的某一个目录下会经常的生成一些垃圾文件,比如访问日志.错误日志.core文件,而你又不想过几分钟就去手动检查一下,那么可以使用定时执行任务的方式来解决.目前我所知道的可以执行定时任务 ...
- android surfaceView 黑屏
最近在做一个viewpager + fragment 切换的页面, 其中一个fragment 打开摄像头,需要surfaceView,但是当切换到这个fragment的前一个个时,这个fragment ...
- iOS UIView 动画浅谈
UIView 等会效果简单实现,哪一个登录页面的demo来举例子吧. + (void)animateWithDuration:(NSTimeInterval)duration animations:( ...