使用Python監(jiān)視指定目錄下文件變更的方法
更新時間:2018年10月15日 10:30:29 作者:曉東邪
今天小編就為大家分享一篇使用Python監(jiān)視指定目錄下文件變更的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
監(jiān)視指定目錄下文件變更。
# -*- coding: utf-8 -*-
# @Author: xiaodong
# @Date: just hide
# @Last Modified by: xiaodong
# @Last Modified time: just hide
import os
import glob
import json
import datetime
from typing import Iterable
"""
監(jiān)視指定目錄下文件變更
"""
def penetrate(root: os.path) -> Iterable:
for ele in glob.glob(os.path.join(root, '*')):
if os.path.isdir(ele):
yield ele
yield from penetrate(os.path.abspath(ele))
else:
yield ele
def update(s: set, exists: bool=False, mode: str='w') -> None or dict :
with open('file_records.json', encoding='utf-8', mode=mode) as file:
if not exists:
json.dump({'datetime': str(datetime.datetime.now()),
'files': list(s)}, file, ensure_ascii=False, indent=10)
else:
return json.load(file)
def main(s: set=set(), root: os.path='.')-> None:
for path in penetrate(root):
s.add(path)
if not os.path.exists('file_records.json'):
update(s)
else:
d = update(None, True, 'r')
files = s - set(d['files'])
files2 = set(d['files']) - s
if files:
print('增加文件: ', files)
if files2:
print('刪除文件: ', files2)
if files or files2:
update(s)
print('更新成功!')
if __name__ == "__main__":
main()
以上這篇使用Python監(jiān)視指定目錄下文件變更的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python設(shè)計模式結(jié)構(gòu)型代理模式
這篇文章主要介紹了Python設(shè)計模式結(jié)構(gòu)型代理模式,代理模式即Proxy?Pattern,為其他對象提供一種代理以控制對這個對象的訪問,下文內(nèi)容詳細介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-02-02
Python 中 Selenium 的 getAttribute()
本文將解釋如何使用Selenium的getAttribute()方法,getAttribute() 方法可以檢索元素屬性,例如錨標記的 href 屬性, 該函數(shù)最初將嘗試返回指定屬性的值,感興趣的朋友跟隨小編一起看看吧2023-11-11
python 使用pygame工具包實現(xiàn)貪吃蛇游戲(多彩版)
這篇文章主要介紹了python 使用pygame工具包實現(xiàn)貪吃蛇游戲,本篇給大家分享的是一個多彩版,通過實例代碼給大家講解,非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10

