1.
os.walk
os.walk()返回一个三元素的tuple:当前路径、子文件夹名称、文件列表。>>>
import
os>>>
def
fun(
path
):...
for
root,
dirs,
files
in
os.walk(
path
):...
for
fn
in
files:...
root,
fn...
>>>
fun(
r'C:\TDDOWNLOAD'
)C:\TDDOWNLOAD
a.txtC:\TDDOWNLOAD
b.txtC:\TDDOWNLOAD\sub1
c.txtC:\TDDOWNLOAD\sub1
d.txt>>>
2.
glob.glob
glob.glob()只接受一个参数,这个参数既代有路径,又代有匹配模式,返回值为一个列表。注意,glob.glob()无法直接穿透子文件夹,需要自己处理:>>>
def
fun(
path
):...
for
fn
in
glob.glob(
path
+
os.sep
+
'*'
):
#
'*'代表匹配所有文件...
if
os.path.isdir(
fn
):
#
如果结果为文件夹...
fun(
fn
)
#
递归...
else:...
fn...
>>>
fun(
r'C:\TDDOWNLOAD'
)C:\TDDOWNLOAD\a.txtC:\TDDOWNLOAD\b.txtC:\TDDOWNLOAD\sub1\c.txtC:\TDDOWNLOAD\sub1\d.txt>>>
'*'为匹配模式,代表匹配所有文件,只有这样才能将子文件夹查出来,以便递归深入,探查下一层的文件。
监控目录 -- 分两个部分: 1. 扫描目录文件, 保持当前状态数据2. 状态数据的比较
import os
import fnmatch
def getfileinfo(filename):
(mode, ino, dev, nlink,
uid, gid, size, atime, mtime, ctime) = os.stat(filename)
return dict(
modifytime=mtime,
createtime=ctime,
size=size,
)
class DirectoryMonitor(object):
def __init__(self, path, fnexp="*.*"):
self.path = path
self.fnexp = fnexp
self.files = {}
self.scan()
def scan(self):
currentfiles = {}
for path, dirs, files in os.walk(self.path):
for f in fnmatch.filter(files, self.fnexp):
fullname = os.path.join(path, f)
currentfiles[fullname] = getfileinfo(fullname)
lastfiles = self.files
self.files = currentfiles
return self.check(lastfiles, currentfiles)
@staticmethod
def check(lastfiles, currfiles):
monitor = {}
newer = {}
for f in set(currfiles) - set(lastfiles):
newer[f] = currfiles[f]
if newer:
monitor["newer"] = newer
deleted = {}
for f in set(lastfiles) - set(currfiles):
deleted[f] = lastfiles[f]
if deleted:
monitor["deleted"] = deleted
changed = {}
for f in set(lastfiles) & set(currfiles):
if lastfiles[f] != currfiles[f]:
changed[f] = currfiles[f]
if changed:
monitor["changed"] = changed
return monitor
def tester():
import time
dm = DirectoryMonitor(r"/home/tim/data", "*.txt")
time.sleep(20)
m = dm.scan()
if m:
print m
if __name__ == "__main__":
tester()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)