defflush(): """獲取目錄下的全部文檔 :return: 返回所有文檔路徑的set """ for fpathe, dirs, fs in os.walk('.'): for f in fs: TEMP.append(os.path.join(fpathe, f))
return set(TEMP)
defscanning(): """ 掃描當前所有新添加的文檔 :return: 返回新添加的文檔set """ TEMP = [] for fpathe, dirs, fs in os.walk('.'): for f in fs: TEMP.append(os.path.join(fpathe, f)) FILE_LIST_NOW = set(TEMP)
return FILE_LIST_NOW - FILE_LIST
if __name__ == '__main__': FILE_LIST = flush() whileTrue: file = scanning() for i in file: try: f = str(open(i, 'rb').read()) # 讀取文檔 if re.search(PROHIBITED, f).group(): # 檢測文檔是否包含指定內容,如果存在,立刻移除,並返回提示 os.remove(i) print('There has find a warning file, and it was remove,the file name is {}'.format(i)) except FileNotFoundError: print('F') except UnicodeDecodeError: print('U') except AttributeError: print('A') ifnot file: # 移除文檔後更新文檔列表 FILE_LIST = flush()