Fundamentals 10 min read

10 Practical Python Scripts for Text File Automation

This article presents ten useful Python automation scripts that demonstrate how to read, compare, filter, merge, re‑encode, extract, count, report, replace, and selectively retrieve lines from text files, helping both developers and everyday users boost productivity.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
10 Practical Python Scripts for Text File Automation

Python’s powerful text‑processing capabilities make routine file operations easy. Below are ten practical scripts, each accompanied by a brief explanation and ready‑to‑run code.

1. Read a text file

# 读取文本文件
def read_file(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        content = file.read()
    return content

file_content = read_file('example.txt')
print(file_content)

This script opens example.txt in UTF‑8 mode, reads its entire content, and prints it.

2. Compare two text files

# 对比两个文本文件是否相同
def compare_files(file1, file2):
    with open(file1, 'r', encoding='utf-8') as f1, open(file2, 'r', encoding='utf-8') as f2:
        return f1.read() == f2.read()

are_equal = compare_files('file1.txt', 'file2.txt')
print(f"两个文件是否相同: {are_equal}")

The script returns True if the two files have identical content, which is useful for version checks.

3. Filter out lines containing a keyword

# 过滤掉包含特定单词的行
def filter_lines(file_path, keyword):
    with open(file_path, 'r', encoding='utf-8') as file:
        lines = [line for line in file if keyword not in line]
    return lines

filtered_lines = filter_lines('example.txt', '不需要的内容')
print("过滤后的内容:")
for line in filtered_lines:
    print(line.strip())

Useful for removing irrelevant log entries or unwanted text.

4. Merge multiple text files

# 合并多个文本文件
def merge_files(output_file, *input_files):
    with open(output_file, 'w', encoding='utf-8') as outfile:
        for file_name in input_files:
            with open(file_name, 'r', encoding='utf-8') as infile:
                outfile.write(infile.read() + "\n")

merge_files('merged.txt', 'file1.txt', 'file2.txt', 'file3.txt')
print("文件合并成功!")

Combines several reports or logs into a single document.

5. Convert file encoding

# 转换文本文件编码
def convert_encoding(input_file, output_file, target_encoding='utf-8'):
    with open(input_file, 'r', encoding='gbk') as infile:
        content = infile.read()
    with open(output_file, 'w', encoding=target_encoding) as outfile:
        outfile.write(content)

convert_encoding('old_file.txt', 'new_file.txt', 'utf-8')
print("文件编码转换成功!")

Helps resolve encoding issues when opening files created in different locales.

6. Extract lines with a specific prefix

# 提取以特定标记开头的行
def extract_data(file_path, prefix):
    with open(file_path, 'r', encoding='utf-8') as file:
        extracted = [line for line in file if line.startswith(prefix)]
    return extracted

data_lines = extract_data('example.txt', '重要信息:')
print("提取的数据:")
for line in data_lines:
    print(line.strip())

Ideal for pulling configuration entries or marked sections from a file.

7. Count word frequencies

from collections import Counter

# 统计文件中的词频
def count_words(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        words = file.read().split()
    return Counter(words)

word_count = count_words('example.txt')
print("词频统计:")
for word, count in word_count.items():
    print(f"{word}: {count}")

Provides insight into the most common terms, useful for writing or text analysis.

8. Generate a simple text report

# 生成简单的文本报告
def generate_report(data, report_file):
    with open(report_file, 'w', encoding='utf-8') as file:
        file.write("文本分析报告\n")
        file.write("=====================\n")
        for item in data.items():
            file.write(f"{item[0]}: {item[1]}\n")

report_data = {'项目A': 10, '项目B': 15}
generate_report(report_data, 'report.txt')
print("报告生成成功!")

Creates a formatted summary that can be shared in meetings or documentation.

9. Replace specific text

# 替换文本文件中的指定内容
def replace_text(file_path, old_string, new_string):
    with open(file_path, 'r', encoding='utf-8') as file:
        content = file.read()
    new_content = content.replace(old_string, new_string)
    with open(file_path, 'w', encoding='utf-8') as file:
        file.write(new_content)

replace_text('example.txt', '旧内容', '新内容')
print("文本内容替换成功!")

Convenient for bulk updates, such as correcting terminology across a document.

10. Extract lines by line numbers

# 按指定行号提取内容
def extract_lines_by_numbers(file_path, line_numbers):
    with open(file_path, 'r', encoding='utf-8') as file:
        lines = file.readlines()
    extracted_lines = [lines[i - 1].strip() for i in line_numbers if 0 < i <= len(lines)]
    return extracted_lines

line_indices = [1, 3, 5]  # 提取第1、3、5行
extracted_content = extract_lines_by_numbers('example.txt', line_indices)

print("提取的行内容:")
for line in extracted_content:
    print(line)

This utility quickly pulls out specific sections, handy for reviewing particular pages of a report.

Together, these examples showcase Python’s versatility in automating everyday text‑file tasks, encouraging readers to experiment and extend the scripts for their own workflows.

PythonAutomationscriptingexamplestext processingfile handling
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.