python3 filter

来源:undefined 2025-06-02 09:42:36 0

如果你需要过滤出字数不少于1000字的文档或文本文件,以下是一个基本的 Python 脚本示例,使用 Python 3 来实现这一功能。此脚本将读取一个目录中的所有文本文件,并将那些字数不少于1000字的文件筛选出来,打印文件名和字数。

import os def count_words(filename): """Count the number of words in a file.""" with open(filename, r, encoding=utf-8) as file: text = file.read() words = text.split() return len(words) def filter_files_by_word_count(directory, min_word_count=1000): """Filter text files in a directory by specifying a minimum word count.""" filtered_files = [] # Iterate over all files in the directory for filename in os.listdir(directory): if filename.endswith(.txt): # Check if the file is a text file filepath = os.path.join(directory, filename) word_count = count_words(filepath) if word_count >= min_word_count: filtered_files.append((filename, word_count)) return filtered_files def main(): directory = /path/to/your/directory # Replace with your directory path min_word_count = 1000 filtered_files = filter_files_by_word_count(directory, min_word_count) if filtered_files: print(f"Files with at least {min_word_count} words:") for file, count in filtered_files: print(f"{file}: {count} words") else: print(f"No files found with at least {min_word_count} words.") if __name__ == "__main__": main()

说明

count_words 函数: 接收一个文件名作为参数,读取该文件并计算其中文字的数量。

filter_files_by_word_count 函数: 接收一个目录名和最小字数作为参数。它遍历目录中的所有文件,使用 count_words 函数计算每个文件的字数。如果文件的字数不少于指定的最小字数(1000),则将文件信息(名称和字数)添加到结果列表中。

main 函数: 设置目录路径和最小字数,然后调用 filter_files_by_word_count 函数获取符合条件的文件,并打印其名称及字数。

使用方法

确保你的文本文件使用 UTF-8 编码,否则你可能需要调整编码设置。 修改 directory 变量,指向包含要检查文件的实际目录路径。 运行该脚本,如果有文件符合条件,该脚本将打印文件名和字数。

请根据需要扩展或修改此脚本,以满足特定需求,如处理不同文件编码、支持子目录遍历等。

上一篇:http body 下一篇:classlist.add

最新文章