2019独角兽企业重金招聘Python工程师标准>>>

Basic

mtime + checksum + directory traversal

Code

#!/usr/bin/env python'''
monitor a directory
print the changed files in the directory
strategy: mtime + checksum
'''import sys
import os
import stat
import cPickle as pickledef print_usage():print 'monitor.py init [dir]'print 'monitor.py check [dir]'print 'monitor.py refresh [dir]'print '[dir] defaults to .'def main(argv=None):if len(argv) == 1:action = argv[0]directory = '.'elif len(argv) == 2:action = argv[0]directory = argv[1]else:print_usage()return 1if action != 'init' and action != 'check' and action != 'refresh':print_usage()return 1directory = os.path.abspath(directory)monitor = Monitor(directory)if action == 'init':monitor.init_dir()return 0elif action == 'check':monitor.check_dir()return 0elif action == 'refresh':monitor.refresh_dir()return 0else:print 'Unexpeceted Error!'return 1class Monitor(object):def __init__(self, directory):self.directory = directorydef construct_cache(self):fileCacheList = []# recursively traverse the directory, cache each files's mtime and checksum# {filename1:(mtime, checksum), filename2:(mtime, checksum), ....}for dirpath, dirnames, filenames in os.walk(self.directory):for f in filenames:if f == '.cache':continue    # exclude .cache filef = os.path.join(dirpath, f)print 'dealing with', ffilecache = FileCache(f)fileCacheList.append(filecache)# dump fileCacheList to .cachecacheFilePath = os.path.abspath(os.path.join(self.directory, '.cache'))with open(cacheFilePath, 'wb') as cacheFile:pickler = pickle.Pickler(cacheFile)for filecache in fileCacheList:pickler.dump(filecache)def init_dir(self):'''init directorycache the mtime and checksum of all files in the directorydump the cache to .cache in the direcotry'''print 'init_dir'self.construct_cache()print 'init', self.directory, 'success'returndef check_dir(self):'''check directory to determine which files have changed'''#       print 'check_dir'# make sure the directory has been initialized# i.e. there's a .cache file under this direcotryfiles = os.listdir(self.directory)if not '.cache' in files:print self.directory, 'has not been initialized yet'return# reconstruct fileCacheListfileCacheList = []cacheFilePath = os.path.abspath(os.path.join(self.directory, '.cache'))with open(cacheFilePath, 'rb') as cache:pickled = pickle.Unpickler(cache)while cache:try:filecache = pickled.load()except:breakif isinstance(filecache, FileCache):fileCacheList.append(filecache)# construct a dict from fileCacheListdictFiles = {}for fc in fileCacheList:dictFiles[fc.filepath] = (fc.mtime, fc.checksum)# traverse the target directory and determine which files have changedfor dirpath, dirnames, filenames in os.walk(self.directory):for f in filenames:if f == '.cache':continue    # exclude .cache filef = os.path.join(dirpath, f)
#                print 'checking', fif f not in dictFiles:print '[ADD]:', felse:           # f in dictFilessmtime = dictFiles[f][0]cmtime = os.stat(f)[stat.ST_MTIME]if cmtime == smtime:passelse:
#                        print 'file %s changed mtime, recompute checksum' % fschecksum = dictFiles[f][1]cchecksum = md5_file(f)if schecksum == cchecksum:passelse:print '[CHANGED]:', f# remove entry f from dictFilesdictFiles.pop(f)# tranverse endedif len(dictFiles) != 0:for f in dictFiles:print '[REMOVE]:', fdef refresh_dir(self):print 'refresh_dir'self.construct_cache()print 'refresh %s success' % self.directorydef md5_file(filename):try:import hashlibm = hashlib.md5()except ImportError:import md5m = md5.new()for line in open(filename):m.update(line)return m.hexdigest()class FileCache(object):def __init__(self, f):self.filepath = os.path.abspath(f)self.mtime = os.stat(f)[stat.ST_MTIME]self.checksum = md5_file(f)def __str__(self):return self.filepath+str(self.mtime)+str(self.checksum)if __name__ == '__main__':main(sys.argv[1:])

Test

chenqi@chenqi-OptiPlex-760:~/mypro/python/monitorDir$ ./monitor.py init
init_dir
dealing with /home/chenqi/mypro/python/monitorDir/monitor.py
dealing with /home/chenqi/mypro/python/monitorDir/monitor.py~
dealing with /home/chenqi/mypro/python/monitorDir/test1/1.txt
dealing with /home/chenqi/mypro/python/monitorDir/test1/2.txt
dealing with /home/chenqi/mypro/python/monitorDir/test1/test_sub1/5.txt
dealing with /home/chenqi/mypro/python/monitorDir/test1/test_sub1/6.txt
dealing with /home/chenqi/mypro/python/monitorDir/test2/4.txt
dealing with /home/chenqi/mypro/python/monitorDir/test2/3.txt
init /home/chenqi/mypro/python/monitorDir success
chenqi@chenqi-OptiPlex-760:~/mypro/python/monitorDir$ touch monitor.py
chenqi@chenqi-OptiPlex-760:~/mypro/python/monitorDir$ ./monitor.py check
chenqi@chenqi-OptiPlex-760:~/mypro/python/monitorDir$ touch 1.txt
chenqi@chenqi-OptiPlex-760:~/mypro/python/monitorDir$ ./monitor.py check
[ADD]: /home/chenqi/mypro/python/monitorDir/1.txt
chenqi@chenqi-OptiPlex-760:~/mypro/python/monitorDir$ ./monitor.py refresh
refresh_dir
dealing with /home/chenqi/mypro/python/monitorDir/1.txt
dealing with /home/chenqi/mypro/python/monitorDir/monitor.py
dealing with /home/chenqi/mypro/python/monitorDir/monitor.py~
dealing with /home/chenqi/mypro/python/monitorDir/test1/1.txt
dealing with /home/chenqi/mypro/python/monitorDir/test1/2.txt
dealing with /home/chenqi/mypro/python/monitorDir/test1/test_sub1/5.txt
dealing with /home/chenqi/mypro/python/monitorDir/test1/test_sub1/6.txt
dealing with /home/chenqi/mypro/python/monitorDir/test2/4.txt
dealing with /home/chenqi/mypro/python/monitorDir/test2/3.txt
refresh /home/chenqi/mypro/python/monitorDir success
chenqi@chenqi-OptiPlex-760:~/mypro/python/monitorDir$ rm 1.txt
chenqi@chenqi-OptiPlex-760:~/mypro/python/monitorDir$ ./monitor.py check
[REMOVE]: /home/chenqi/mypro/python/monitorDir/1.txt
chenqi@chenqi-OptiPlex-760:~/mypro/python/monitorDir$

转载于:https://my.oschina.net/u/158589/blog/75590

Python: Monitoring a Directory相关推荐

  1. python操作Active Directory

    该说明基于python3和ldap3 2.5,只在Windows2012 R2的Active Directory上测试过 常见操作 1. 连接 server = Server(ldap_url, us ...

  2. Cannot run program‘C:\Anaconda3\envs\...\python.exe’(in directory):CreateProcess error=2,系统找不到指定文件

    如图,因为之前运行成功过,过了许久再运行就出现上图的错误,sad... 网查了许久都没有解决我的问题. 突然,自己想到了,可能是编译器的路径被换了的问题,然后自然就想到了首次运行会生成一个记录文档,类 ...

  3. bash: /usr/bin/python: Is a directory

  4. Pycharm报错:Error running ‘‘: Cannot run program “\python.exe“ (in directory ““)系统找不到指定文件夹?

    问题报错 报错原因: 因为我的工程在之前的路径下运行过,所以PyCharm保存过项目的一些信息,而这些信息还是在之前路径下的配置. 解决办法 1.在该项目文件夹下找到一个叫.idea的文件夹.(若没有 ...

  5. Python Console报错:Cannot start process, the working directory does not exist

    设置默认工作路径就会好了. File > Settings > Build, Execution, Deployment > Console > Python Console ...

  6. 解决 python pip install安装速度慢| 版本低|跨过个别错误包

    pip2 install keras -i http://pypi.douban.com/simple --trusted-host pypi.douban.com 跨过个别错误包: pip批量安装跳 ...

  7. Python相对导入导致SystemError的解决方案(译)

    原文出处: http://stackoverflow.com/   译文出处:yibohu1899 这个问题是如何解决在相对导入的时候,如果出现'System Error'的时候的解决方案.顺带一提, ...

  8. python管理包管理工具pip和conda使用,及使用pip和conda创建虚拟环境

    python管理包管理工具pip使用,及使用pip创建虚拟环境 文章目录: 1 pip的使用 1.1 pip的基础使用 1.1.1 pip安装库包(pip install) 1.1.2 pip卸载库包 ...

  9. Python常见问题(6):Python在Windows上的问题 Python on Windows FAQ

    Contents Python on Windows FAQ How do I run a Python program under Windows? How do I make Python scr ...

最新文章

  1. .net调试插件sosex的mk命令显示调用堆栈
  2. ASP.Net TextBox控件只允许输入数字
  3. python代码用c语言封装_使用C语言扩展Python程序的简单入门指引
  4. QT的QVectorIterator类的使用
  5. 几款好用超赞的Google Chrome插件
  6. go语言的书籍的淘宝调查
  7. java builder pool_每周10道Java面试题:String, String Pool, StringBuilder
  8. Android 10分钟集成极光推送
  9. linux经典命令-Web服务器管理
  10. 【软件测试】单元测试是软件测试的最基础环节
  11. 一个简单的重命名,在git中也有这么多学问
  12. HDU2837 Calculation(指数循环节)题解
  13. vb 字符串和数字相互转换函数
  14. 超详细的80个Python入门实例,代码清晰拿来即用,学习提升必备
  15. 又是一年“剁手”时,AI一下更优惠?
  16. HITB CTF 2018 gundam 做题笔记
  17. N4110解决WIN10黑屏问题
  18. python语音识别库kaldi_Kaldi 使用 DFSMN 训练语音模型
  19. 与Windows更新的抗争-取消Windows系统自动更新
  20. SONET、SDH、POS简介

热门文章

  1. 谷歌AI涉足艺术、太空、外科手术,再强调AI七原则
  2. 毕马威:2018全球科技创新报告(附PDF下载)
  3. 俄罗斯智库 | 人工智能在军事领域的发展现状及应用前景
  4. GIF动图之父Stephen Wilhite去世,享年74岁
  5. 太真实!当程序员接手了新项目...... | 每日趣闻
  6. 大厂围猎春招,年轻人却卸甲出逃
  7. 6年从华人首富到破产,这可能是史上最惨的接班案例
  8. 羡慕嫉妒!看了腾讯月收入 8 万 的支出账单不恨了 | 每日趣闻
  9. 漫画:什么是 “代理模式” ?
  10. 谷歌知名前 AI 研究员无辜被裁,CEO:调查!