网站首页 > 技术文章 正文
Python处理文本的优势主要体现在其简洁性、功能强大和灵活性。具体来说,Python提供了丰富的库和工具,使得对文件的读写、处理变得轻而易举。
简洁的文件操作接口
- Python通过内置的open()函数,可以方便地打开文件进行读取或写入。例如,使用open('example.txt', 'r')可以快速打开一个文件进行读取。
- 使用上下文管理器(with语句),可在操作完成后自动关闭文件,避免资源泄露。
灵活的文件类型处理
- Python不仅支持文本文件的处理,还能处理二进制文件、JSON、XML等多种类型的文件,这得益于其丰富的标准库和第三方库。
跨平台兼容性
- Python的文件处理功能在不同的操作系统上(如Windows、macOS、Linux)都能无缝运行,这为跨平台应用程序的开发提供了极大的便利。
为了更直观了解Python处理文本的优势,本文给出25个Python常用的案例。
- 打开文件并读取内容
# 打开文件并读取内容
with open('example.txt', 'r') as file:
content = file.read()
print(content)
- 写入文件
# 写入文件
with open('example.txt', 'w') as file:
file.write('Hello, World!')
- 追加内容到文件
# 追加内容到文件
with open('example.txt', 'a') as file:
file.write('
New line')
- 读取文件的某一行
# 读取文件的某一行
with open('example.txt', 'r') as file:
line = file.readlines()[0]
print(line)
- 逐行读取文件
# 逐行读取文件
with open('example.txt', 'r') as file:
for line in file:
print(line.strip())
- 使用fileinput模块修改文件内容
import fileinput
# 使用fileinput模块修改文件内容
for line in fileinput.input('example.txt', inplace=True):
print(line.replace('old_text', 'new_text'), end='')
- 复制文件
# 复制文件
with open('example.txt', 'r') as source, open('copy.txt', 'w') as destination:
destination.write(source.read())
- 移动文件
# 移动文件
import os
os.rename('example.txt', 'moved_example.txt')
- 删除文件
# 删除文件
import os
os.remove('example.txt')
- 重命名文件
# 重命名文件
import os
os.rename('example.txt', 'renamed_example.txt')
- 创建文件夹
# 创建文件夹
import os
os.mkdir('new_folder')
- 删除文件夹
# 删除文件夹
import os
os.rmdir('new_folder')
- 遍历文件夹中的所有文件
# 遍历文件夹中的所有文件
import os
for file in os.listdir('.'):
print(file)
- 获取文件大小
# 获取文件大小
import os
file_size = os.path.getsize('example.txt')
print(file_size)
- 检查文件是否存在
# 检查文件是否存在
import os
if os.path.exists('example.txt'):
print("文件存在")
else:
print("文件不存在")
- 读取文件并去除空格
# 读取文件并去除空格
with open('example.txt', 'r') as file:
content = file.read().strip()
print(content)
- 读取文件并按行分割
# 读取文件并按行分割
with open('example.txt', 'r') as file:
lines = file.read().splitlines()
print(lines)
- 读取文件并统计行数
# 读取文件并统计行数
with open('example.txt', 'r') as file:
line_count = sum(1 for line in file)
print(line_count)
- 读取文件并查找特定文本
# 读取文件并查找特定文本
with open('example.txt', 'r') as file:
if 'target_text' in file.read():
print("找到目标文本")
else:
print("未找到目标文本")
- 将文件内容转换为列表
# 将文件内容转换为列表
with open('example.txt', 'r') as file:
content_list = file.read().split()
print(content_list)
- 将文件内容转换为字典
# 将文件内容转换为字典
with open('example.txt', 'r') as file:
content_dict = dict(line.split() for line in file)
print(content_dict)
- 写入多行内容到文件
# 写入多行内容到文件
with open('example.txt', 'w') as file:
file.write('Line 1
Line 2
Line 3')
- 使用pathlib模块复制文件
# 使用pathlib模块复制文件
from pathlib import Path
source = Path('example.txt')
destination = Path('copy.txt')
source.replace(destination)
- 使用pathlib模块移动文件
# 使用pathlib模块移动文件
from pathlib import Path
file = Path('example.txt')
file.rename('moved_example.txt')
- 使用pathlib模块创建和删除文件夹
# 使用pathlib模块创建和删除文件夹
from pathlib import Path
# 创建文件夹
new_folder = Path('new_folder')
new_folder.mkdir()
# 删除文件夹
new_folder.rmdir()
本文这些案例可以帮助大家更高效地处理文本文件,提高大家的Python编程技能。
猜你喜欢
- 2025-06-28 python文件操作常用方法整理(python文件操作总结)
- 2025-06-28 Python3查看文件是否存在,以及读、写与执行的属性
- 2025-06-28 Python 中的前缀删除操作全指南(python删除后缀)
- 2025-06-28 Python教程(18)——python文件操作详解
- 2025-06-28 你的手机是不是经常提示存储空间不足?Python帮你清理重复文件
- 2025-06-28 用于清理数据的 5 个简单有效 Python 脚本
- 2025-06-28 「万能Python」-15-文件处理(关于python对文件的处理)
- 2025-06-28 Linux系统自带Python2&yum的卸载及重装
- 2025-06-28 Linux 下海量文件删除方法效率对比,最慢的竟然是 rm
- 2025-06-28 python应用-shutil详解文件复制/移动/删除功能
- 272℃Python短文,Python中的嵌套条件语句(六)
- 271℃python笔记:for循环嵌套。end=""的作用,图形打印
- 269℃PythonNet:实现Python与.Net代码相互调用!
- 264℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 264℃Python实现字符串小写转大写并写入文件
- 123℃原来2025是完美的平方年,一起探索六种平方的算吧
- 104℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 99℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
-
- Python数据分析实战-dataframe分组提取每一组的首条记录
- 如何使用Python将多个excel文件数据快速汇总?
- 「Python数据分析」Pandas进阶,使用groupby分组聚合数据(二)
- 还在熬夜合并30个Excel 3个案例,带你用Python玩转Excel高阶操作
- python数据分析实战:pandas分组聚合-自定义聚合函数
- Python 知识点 #31 - 分组和聚(python分层聚类)
- 人生苦短,自学 python——pandas 的分组操作
- 利用Python进行数据分组/数据透视表
- 超实用!用Python快速实现数据分组统计与透视表
- Python 之 Pandas:数据分组聚合统计的魔法秘籍
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)