12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- """
- https://www.cyberforum.ru/python-graphics/thread2712141.html
- """
- import os
- from difflib import SequenceMatcher
- from tqdm import tqdm
- # директория файла
- BASE_DIR = os.path.abspath(os.path.dirname(__file__))
- print()
- files_paths = []
- dirs = os.listdir(BASE_DIR)
- for dir in dirs:
- dir_path = os.path.join(BASE_DIR, dir)
- if os.path.isdir(dir_path) and (dir != "__pycache__"):
- files = os.listdir(dir_path)
- for file in files:
- file_path = os.path.join(BASE_DIR, dir, file)
- filename, fileext = os.path.splitext(file)
- if os.path.isfile(file_path) and (fileext=='.md'):
- files_paths.append(file_path)
- print(f"Всего файлов: {len(files_paths)}")
- for file_1 in tqdm(files_paths):
- for file_2 in files_paths:
- if (file_1 != file_2):
- try:
- with open(file_1, encoding="utf-8") as f_1:
- str1 = f_1.read()
- except:
- with open(file_1, encoding="cp1251") as f_1:
- str1 = f_1.read()
- f_1.close()
- with open(file_1, 'w', encoding="utf-8") as f_1:
- f_1.write(str1)
- f_1.close()
-
- try:
- with open(file_2, encoding="utf-8") as f_2:
- str2 = f_2.read()
- except:
- print()
- print(file_2)
- print("Неверная кодировка")
- with open(file_2, encoding="cp1251") as f_2:
- str2 = f_2.read()
- f_2.close()
- with open(file_2, 'w', encoding="utf-8") as f_2:
- f_2.write(str2)
- f_2.close()
- ratio = int(SequenceMatcher(None, str1.lower(), str2.lower()).ratio() * 100)
- if (ratio > 70):
- print(file_2)
- print(file_1)
- print(f"ratio = {ratio}")
- print()
- print("success")
|