Python开发【笔记】:获取目录下所有文件
获取文件
import os def sub_dirs(rdir):
li = os.listdir(rdir)
return li def main(rdir):
content = sub_dirs(rdir)
for i in content:
i = os.path.join(rdir,i)
if os.path.isdir(i):
main(i)
else:
print(i) main('/home/tvrecord')
定时删除目录下时间大于10天的文件
#!/usr/bin/env python
# -*- coding:utf-8 -*- import os
import time
import sys
import logging
import datetime
import threading # 检测目录
ROOT_DIR = '/data2/log2/'
# 保留10天文件
SAVE_DATE = 10
# 开始时间小时
START_HOUR = 2
# 开始时间分钟
START_MINUTE = 00 def daemon():
import os
# create - fork 1
try:
pid = os.fork()
if pid > 0:
return pid
except OSError as error:
logging.error('fork #1 failed: %d (%s)' % (error.errno, error.strerror))
return -1
# it separates the son from the father
os.chdir('/opt/pbx')
os.setsid()
os.umask(0)
# create - fork 2
try:
pid = os.fork()
if pid > 0:
return pid
except OSError as error:
logging.error('fork #2 failed: %d (%s)' % (error.errno, error.strerror))
return -1
sys.stdout.flush()
sys.stderr.flush()
si = open("/dev/null", 'r')
so = open("/dev/null", 'ab')
se = open("/dev/null", 'ab', 0)
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
return 0 def set_log():
logging.basicConfig(filename="/data2/log2/delete_log", level=logging.INFO,
format="%(asctime)s [%(filename)s:%(lineno)d] %(message)s", datefmt="%m/%d/%Y %H:%M:%S [%A]") def sub_dirs(rdir):
'''
获取目录下所有文件
:param rdir:
:return:
'''
li = os.listdir(rdir)
return li def get_files(rdir):
'''
获取文件路径
:param rdir:
:return:/data2/log2/ariservice7/ari.log.10
'''
content = sub_dirs(rdir)
for i in content:
i = os.path.join(rdir, i)
if os.path.isdir(i):
yield from get_files(i)
else:
yield i def timer():
'''
设置定时
:return:
'''
_date = datetime.datetime.now()
excute_time = '%s %s:%s' % (time.strftime('%Y-%m-%d'), START_HOUR, START_MINUTE)
excute_time = time.strptime(excute_time, '%Y-%m-%d %H:%M')
excute_time = time.mktime(excute_time)
if _date.hour > START_HOUR:
excute_time += 24 * 60 * 60
elif _date.hour == START_HOUR and _date.minute >= START_MINUTE:
excute_time += 24 * 60 * 60
logging.info('timer wait %s', excute_time - time.time())
timer = threading.Timer(excute_time - time.time(), execute, )
timer.start() def execute():
'''
主任务
:return:
'''
delete_time = time.time() - 24*60*60*SAVE_DATE
logging.info('delete time[%s]',delete_time)
for file in get_files(ROOT_DIR):
file_time = os.path.getmtime(file)
logging.info('prepare delete file[%s] delete_time[%s] file_time[%s]',file,delete_time,file_time)
if file_time < delete_time:
os.remove(file)
logging.info('deleted file[%s]',file)
time.sleep(1)
timer() def main():
set_log()
pid = daemon()
if pid:
return pid
execute() if __name__ == '__main__':
main()
Python开发【笔记】:获取目录下所有文件的更多相关文章
- 取CPU序列号,获取网卡,取硬盘系列号,获取目录下的文件,强制删除目录
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- 阿里云OSS 获取目录下所有文件
public class AliyunHandle { public static string accessKeyId = "a1uI5xxxxxxxxxrP4H"; publi ...
- Golang获取目录下的文件及目录信息
一.获取当前目录下的文件或目录信息(不包含多级子目录) func main() { pwd,_ := os.Getwd() //获取当前目录 //获取文件或目录相关信息 fileInfoList ...
- C/C++ 获取目录下的文件列表信息
在C/C++编程时,需要获取目录下面的文件列表信息. 1.数据结构 struct dirent { long d_ino; /* inode number 索引 ...
- python递归获取目录下指定文件
获取一个目录下所有指定格式的文件是实际生产中常见需求. import os #递归获取一个目录下所有的指定格式的文件 def get_jsonfile(path,file_list): dir_lis ...
- shell获取目录下所有文件夹的名称并输出
获取指定目录/usr/下所有文件夹的名称并输出: shell代码: #!/bin/bash #方法一 dir=$(ls -l /usr/ |awk '/^d/ {print $NF}') for i ...
- File类 递归 获取目录下所有文件文件夹
package com.xiwi; import java.io.*; import java.util.*; class file{ public static void main(String a ...
- python获取目录下所有文件
#方法1:使用os.listdir import os for filename in os.listdir(r'c:\\windows'): print filename #方法2:使用glob模块 ...
- php递归获取目录下所有文件
<?php function getFileList($dir){ $dir=iconv("utf-8","gb2312",$dir); if ($hea ...
随机推荐
- 【原】在Matplotlib绘图过程中设置X轴的刻度和显示文本
使用Matplotlib进行绘图时,当x轴的数据太多的时候,就需要设置x轴的刻度和显示文本,关键代码如下: 绘图结果如下:
- net use错误原因解决(精辟)(转)
(1)"发生系统错误 1326. 登录失败: 未知的用户名或错误密码." 在远程机的"控制面板-文件夹选项-查看-简单的文件共享",去掉选取,然后再尝试连接 ...
- Java编程的逻辑 (86) - 动态代理
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...
- 占位 Bootstrap
中文网 http://www.bootcss.com/
- 基于Java的数据采集(三)
<基于Java的数据采集(一)>:http://www.cnblogs.com/lichenwei/p/3904715.html <基于Java的数据采集(二)>:http:/ ...
- Apache Nginx URL 地址 重写
URL重写这东西在工作中用了很多次了,但每次都忘记了要记得把知道的积累下来. 哎,要么认为没必要,要么就是没时间?! 一.Apache 篇 官方地址:http://man.chinaunix.net/ ...
- /usr/bin/ld: cannot find -lncurses是咋回事?
你的系統是32位的還是64位的? 如果是32位的就用:sudo apt-get install libncurses5-dev 如果是64位的,就用:sudo apt-get install lib3 ...
- iOS - UILabel添加图片之富文本的简单应用
//创建富文本 NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@" ...
- hadoop程序实例
安装了Eclipse及hadoop-eclipse-plugin后学着<hadoop权威指南>中的气温例子写了一个输出气温的程序,数据是我自己简单写的,但是输出却不是我预想的,这中间还有很 ...
- C++编程相关工具
1 文档类 (1) Doxygen 参考站点:http://www.doxygen.org Doxygen是一种适合C风格语言(如C++.C.IDL.Java甚至包括C#和PHP)的.开放源码的 ...