文件夹名字修改前
文件夹名字修改后效果
检索结束
代码
import os
path1 = './test/'
i = 1
for category in os.listdir(path1):
folder_name = 'FreshOffTheBoat_' + str(i)
folder_path = os.path.join(path1, folder_name)
os.rename(os.path.join(path1, category), folder_path)
file_path = folder_path + '/'
j = 1
for file in os.listdir(file_path):
file_name = 'FreshOffTheBoat_' + str(i) + '_' + str(j) + '.txt'
new_file_name = os.path.join(file_path, file_name)
os.rename(os.path.join(file_path, file), new_file_name)
print(new_file_name)
j += 1
i += 1
print("End")
import re
a = ["黄家","连锁店"]
num = [0]*len(a)
with open("./test/FreshOffTheBoat_1/FreshOffTheBoat_1_1.txt", "r", encoding="UTF-16") as f:
my_str = f.read()
my_list = my_str.split('>')
for line in my_list:
print(line)
for i in range(len(a)):
if line.find(a[i]) >=0 :
num[i] += 1
print(f"'{a[0]}'出现的次数有{num[0]}次")
print(f"'{a[1]}'出现的次数有{num[1]}次")
import os
import codecs
a = ['春节', '皮蛋', '书法']
num = [0]*len(a)
path1 = './test/'
for category in os.listdir(path1):
dir_path = path1 + category + '/'
for file in os.listdir(dir_path):
file_path = dir_path + file
with open(file_path, "r", encoding="UTF-8") as f:
count = 0
my_str = f.read()
my_list = my_str.split('>')
my_list = str(my_list)
my_list = my_list.split(':')
for line in my_list:
for i in range(len(a)):
if line.find(a[i]) >= 0:
num[i] += 1
count += 1
print("在{}文件{}一句中找到检索词'{}'".format(file, line, a[i]))
print('\n')
print("---------------------完成对{}检索-----------------------".format(file_path))
print('\n')
print("已完成所有文件检索!")
print('\n')
for i in range(len(a)):
print(f"'{a[i]}'出现的总次数:{num[i]}次")
print("End")