Python File Handling: Common Text Processing Scripts
This article presents a comprehensive collection of Python file handling examples, covering basic reading and writing, appending, line extraction, file copying, moving, deletion, directory management, and advanced text processing techniques such as searching, replacing, and statistical analysis, all illustrated with clear code snippets.
Python's advantages in text processing lie in its simplicity, powerful functionality, and flexibility, offering rich libraries and tools that make file reading and writing effortless.
1. Simple file operation interface
# 打开文件并读取内容
with open('example.txt', 'r', encoding='utf-8') as file:
content = file.read()
print("文件内容:")
print(content)Output (assuming example.txt contains):
Hello, world!
This is a test file.
File content:
Hello, world!
This is a test file.
2. Write to file
# 写入文件
with open('example.txt', 'w', encoding='utf-8') as file:
file.write('Hello, world!')Result: example.txt now contains "Hello, world!".
3. Append content to file
# 追加内容到文件
with open('example.txt', 'a', encoding='utf-8') as file:
file.write('\nNew line')Result: example.txt now contains:
Hello, world!
New line
4. Read a specific line from file
# 读取文件的某一行
with open('example.txt', 'r', encoding='utf-8') as file:
line = file.readlines()[0]
print("第一行内容:")
print(line)Output: "Hello, world!"
5. Read file line by line
# 逐行读取文件
with open('example.txt', 'r', encoding='utf-8') as file:
for line in file:
print("行内容:", line.strip())Output:
行内容: Hello, world!
行内容: New line
6. Modify file content using fileinput module
import fileinput
# 使用 fileinput 模块修改文件内容
for line in fileinput.input('example.txt', inplace=True):
print(line.replace('旧文本', '新文本'), end='')Result: the old text in example.txt is replaced with new text.
7. Copy file
# 复制文件
with open('example.txt', 'r', encoding='utf-8') as source, open('copy.txt', 'w', encoding='utf-8') as destination:
destination.write(source.read())
print("文件已复制")Output: "File copied".
8. Move file
# 移动文件
import os
os.rename('example.txt', 'moved_example.txt')
print("文件已移动")Output: "File moved".
9. Delete file
# 删除文件
import os
os.remove('moved_example.txt')
print("文件已删除")Output: "File deleted".
10. Rename file
# 重命名文件
import os
os.rename('example.txt', 'renamed_example.txt')
print("文件已重命名")Output: "File renamed".
11. Create folder
# 创建文件夹
import os
os.mkdir('new_folder')
print("文件夹已创建")Output: "Folder created".
12. Delete folder
# 删除文件夹
import os
os.rmdir('new_folder')
print("文件夹已删除")Output: "Folder deleted".
13. Traverse all files in a folder
# 遍历文件夹中的所有文件
import os
for file in os.listdir('.'):
print("文件/文件夹名称:", file)Output shows the names of files and folders in the current directory.
14. Get file size
# 获取文件大小
import os
file_size = os.path.getsize('example.txt')
print("文件大小:", file_size, "字节")Output: file size in bytes (e.g., 23 bytes).
15. Check if file exists
# 检查文件是否存在
import os
if os.path.exists('example.txt'):
print("文件存在")
else:
print("文件不存在")Output: "File exists".
16. Read file and strip whitespace
# 读取文件并去除空格
with open('example.txt', 'r', encoding='utf-8') as file:
content = file.read().strip()
print("去除空格后的文件内容:")
print(content)Output shows the file content without leading/trailing spaces.
17. Read file and split into lines
# 读取文件并按行分割
with open('example.txt', 'r', encoding='utf-8') as file:
lines = file.read().splitlines()
print("按行分割后的文件内容:")
print(lines)Output: a Python list of lines.
18. Count number of lines in a file
# 读取文件并统计行数
with open('example.txt', 'r', encoding='utf-8') as file:
line_count = sum(1 for line in file)
print("文件行数:", line_count)Output: total line count (e.g., 2).
19. Search for specific text in a file
# 读取文件并查找特定文本
with open('example.txt', 'r', encoding='utf-8') as file:
if 'target text' in file.read():
print("找到目标文本")
else:
print("未找到目标文本")Output: "Found target text" if present.
20. Convert file content to a list
# 将文件内容转换为列表
with open('example.txt', 'r', encoding='utf-8') as file:
content_list = file.read().split()
print("文件内容转换为列表:")
print(content_list)Output: a list of words from the file.
21. Convert file content to a dictionary
# 将文件内容转换为字典
with open('example.txt', 'r', encoding='utf-8') as file:
content_dict = dict(line.split() for line in file)
print("文件内容转换为字典:")
print(content_dict)Output: a dictionary built from key‑value pairs in the file.
22. Write multiple lines to a file
# 写入多行内容到文件
with open('example.txt', 'w', encoding='utf-8') as file:
file.write('Line1\nLine2\nLine3')Result: example.txt now contains three lines.
23. Copy file using pathlib
# 使用 pathlib 模块复制文件
from pathlib import Path
source = Path('example.txt')
destination = Path('copy.txt')
source.replace(destination)
print("文件已复制")Output: "File copied".
24. Move file using pathlib
# 使用 pathlib 模块移动文件
from pathlib import Path
file = Path('example.txt')
file.rename('moved_example.txt')
print("文件已移动")Output: "File moved".
25. Create and delete folder using pathlib
# 使用 pathlib 模块创建和删除文件夹
from pathlib import Path
# 创建文件夹
new_folder = Path('new_folder')
new_folder.mkdir()
print("文件夹已创建")
# 删除文件夹
new_folder.rmdir()
print("文件夹已删除")Outputs: "Folder created" and then "Folder deleted".
26. Replace specific text in a file
# 读取文件并替换特定文本
with open('example.txt', 'r', encoding='utf-8') as file:
content = file.read()
content = content.replace('old text', 'new text')
with open('example.txt', 'w', encoding='utf-8') as file:
file.write(content)
print("文本已替换")Output: "Text replaced" after the substitution.
27. Count word frequency in a file
# 读取文件并统计单词频率
from collections import Counter
with open('example.txt', 'r', encoding='utf-8') as file:
content = file.read()
word_count = Counter(content.split())
print("单词频率统计:")
print(word_count)Output: a Counter object showing each word's occurrence.
28. Extract a specific line from a file
# 读取文件并提取特定行
with open('example.txt', 'r', encoding='utf-8') as file:
lines = file.readlines()
specific_line = lines[1] # extract second line
print("特定行内容:")
print(specific_line)Output: the content of the second line.
29. Extract a specific column from a file
# 读取文件并提取特定列
with open('example.txt', 'r', encoding='utf-8') as file:
lines = file.readlines()
column_data = [line.split()[1] for line in lines] # extract second column
print("特定列内容:")
print(column_data)Output: a list containing the second column of each line.
30. Filter lines by a condition
# 读取文件并按特定条件过滤行
with open('example.txt', 'r', encoding='utf-8') as file:
lines = file.readlines()
filtered_lines = [line for line in lines if 'target text' in line]
print("过滤后的行内容:")
print(filtered_lines)Output: a list of lines that contain the target text.
In summary, the above Python scripts cover a wide range of common text processing tasks, from basic file I/O to more advanced operations such as searching, replacing, and statistical analysis, providing practical examples for efficient handling of text files.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.