使用Python監(jiān)視指定目錄下文件變更的方法
更新時(shí)間:2018年10月15日 10:30:29 作者:曉東邪
今天小編就為大家分享一篇使用Python監(jiān)視指定目錄下文件變更的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
監(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)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
pycharm配置pyqt5-tools開發(fā)環(huán)境的方法步驟
這篇文章主要介紹了pycharm配置pyqt5-tools開發(fā)環(huán)境的方法步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02
Python字符串本身作為bytes進(jìn)行解碼的問題
這篇文章主要介紹了解決Python字符串本身作為bytes進(jìn)行解碼的問題,文末給大家補(bǔ)充介紹了,Python字符串如何轉(zhuǎn)為bytes對象?Python字符串和bytes類型怎么互轉(zhuǎn),需要的朋友可以參考下2022-11-11
python基礎(chǔ)教程之實(shí)現(xiàn)石頭剪刀布游戲示例
使用PYTHON設(shè)計(jì)一個(gè)"石頭,剪子,布"游戲,有時(shí)又叫"Rochambeau",下面是實(shí)現(xiàn)方法,需要的朋友可以參考下2014-02-02
python使用正則搜索字符串或文件中的浮點(diǎn)數(shù)代碼實(shí)例
這篇文章主要介紹了python使用正則搜索字符串或文件中的浮點(diǎn)數(shù)代碼實(shí)例,同時(shí)包含一個(gè)讀寫到文件功能,需要的朋友可以參考下2014-07-07
PIP安裝python包出現(xiàn)超時(shí)問題的解決
這篇文章主要介紹了PIP安裝python包出現(xiàn)超時(shí)問題的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
python網(wǎng)頁請求urllib2模塊簡單封裝代碼
這篇文章主要分享一個(gè)python網(wǎng)頁請求模塊urllib2模塊的簡單封裝代碼,有需要的朋友參考下2014-02-02

