import os
import shutil
import re
import codecs
src_dir_path = r'C:\Users\Administrator\PycharmProjects\源文件'
first_filer = r'C:\Users\Administrator\PycharmProjects\存放分类文件的文件夹\first_filer '
second_filer = r''
if os.path.exists(src_dir_path):
for file in os.listdir(src_dir_path):
if os.path.isfile(src_dir_path+'/'+file):
f = open(src_dir_path+"\\"+file,'r',encoding='utf-8').read()
if re.search("某一字符串", f):
print('找到包含"' + '某一字符串' + '"字符的文件,绝对路径为----->' + src_dir_path + '/' + file)
if not os.path.exists(first_filer ):
os.mkdir(first_filer , 1)
os.rename(src_dir_path + '\\' + file, first_filer + '\\' + file)
else:
os.rename(src_dir_path + '\\' + file, first_filer + '\\' + file)
if re.search('某一字符串2',f):
print('找到包含"' + '某一字符串2' + '"字符的文件,绝对路径为----->' + src_dir_path + '/' + file)
if not os.path.exists(second_filer ):
os.mkdir(second_filer , 1)
os.rename(src_dir_path + '\\' + file, second_filer + '\\' + file)
else:
os.rename(src_dir_path + '\\' + file, second_filer + '\\' + file)