cdsn代码处理:简单查找重复文本文件

简单查找重复文本文件

声明这是最初


我的提问给个文本分类清单

input查找文件夹下
.py .txt  .excel .word
一模一样的文本

不是找文件名
找相同格式下的文件文本是否一样
文件单独复制到文件夹下

两个文件全部复制到文件夹下
print 打印相同文本文件的名字
比如查找到了3.py与4.5.是.py文件中的文本文件相同
则print

相同.py文本文件为

1. 
3.py
4.5.是.py
统计他们的字数:30个
标点符号个数:等等全部相同

原代码


import os
import shutil
from filecmp import cmp

# 统计文件的字数和标点符号个数
def count_chars_and_punctuations(filepath):
    with open(filepath, "r", encoding="utf-8") as f:
        text = f.read()
        # 统计字数
        char_count = len(text)
        # 统计标点符号个数
        punct_count = sum([1 for char in text if char in ",。!?;:“”‘’【】《》(){}【】、|——"])
    return char_count, punct_count

# 查找指定文件夹下指定扩展名的文件,并比较它们的文本内容是否一样,若一样则将它们复制到Duplicates文件夹中
def find_duplicate_files(folder_path, extensions):
    file_paths = []
    for extension in extensions:
        # 遍历文件夹中指定扩展名的文件
        files = [os.path.join(folder_path, file) for file in os.listdir(folder_path) if file.endswith(extension)]
        file_paths.extend(files)

    texts = {}  # 记录已经出现过的文件的文本内容和文件路径

    # 比较文件文本内容是否一样,并将一样的文件复制到Duplicates文件夹中
    for file_path in file_paths:
        with open(file_path, "r", encoding="utf-8") as f:
            text = f.read()

        # 若文本内容已经出现过,则复制该文件到Duplicates文件夹中
        if text in texts:
            duplicated_file = os.path.join(folder_path, "Duplicates", os.path.basename(file_path))
            shutil.copyfile(file_path, duplicated_file)
        else:
            texts[text] = file_path

    # 统计重复文件中的字数和标点符号个数
    char_count, punct_count = 0, 0
    duplicated_files = [os.path.join(folder_path, "Duplicates", file) for file in os.listdir(os.path.join(folder_path, "Duplicates"))]
    for file in duplicated_files:
        char, punct = count_chars_and_punctuations(file)
        char_count += char
        punct_count += punct

    print("\n相同文本的文件名:")
    for text, file_path in texts.items():
        print(os.path.basename(file_path))
    
    print(f"\n统计重复文件中的字数和标点符号个数:\n\t字数:{char_count}个\n\t标点符号个数:{punct_count}个")

folder_path = input("请输入文件夹路径:")
extensions_input = input("请输入要查找的文件扩展名,以空格分隔(例如:.py .txt .excel .word):")
extensions = extensions_input.split()

# 创建用于存放重复文件的文件夹
duplicates_folder = os.path.join(folder_path, "Duplicates")
if not os.path.exists(duplicates_folder):
    os.makedirs(duplicates_folder)

find_duplicate_files(folder_path, extensions)


运行结果,不知真假

/data/user/0/org.qpython.qpy/files/bin/qpy thon3.sh "/storage/emulated/0/qpython/编号 文件.py3.5.py" && exit
python/编号文件.py3.5.py" && exit        < 请输入文件夹路径:/storage/emulated/0/数据 中心/文件夹/py
请输入要查找的文件扩展名,以空格分隔(例如 :.py .txt .excel .word):.py

相同文本的文件名:
2. 表格操作行列1.3.2单独1.2先输入列,再输入行1.1.5.a.1.py
3. 表格操作行列1.3.2单独1.2先输入列,再输入行1.1.5.a.py
5. 表格操作行列1.3.2单独1.2先输入列,再输入行1.1.5.1.py
6. 表格操
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值