Python提供了内置的open()函数来操作文件。
# 打开文件
file = open("example.txt", "r")
content = file.read()
file.close() # 必须关闭文件
# 推荐使用with语句(自动关闭)
with open("example.txt", "r") as file:
content = file.read()
# 文件自动关闭
r:只读模式(默认)w:写入模式(覆盖原文件)a:追加模式(在文件末尾添加)r+:读写模式b:二进制模式(如rb、wb)x:独占创建模式(文件存在则失败)with open("data.txt", "r", encoding="utf-8") as file:
content = file.read()
print(content)
# 方法1:readline()
with open("data.txt", "r", encoding="utf-8") as file:
line = file.readline()
while line:
print(line.strip()) # strip()去除换行符
line = file.readline()
# 方法2:readlines()
with open("data.txt", "r", encoding="utf-8") as file:
lines = file.readlines()
for line in lines:
print(line.strip())
# 方法3:迭代文件对象(推荐)
with open("data.txt", "r", encoding="utf-8") as file:
for line in file:
print(line.strip())
with open("data.txt", "r", encoding="utf-8") as file:
chunk = file.read(100) # 读取前100个字符
print(chunk)
# 覆盖写入
with open("output.txt", "w", encoding="utf-8") as file:
file.write("Hello, Python!\n")
file.write("这是第二行\n")
# 追加写入
with open("output.txt", "a", encoding="utf-8") as file:
file.write("追加的内容\n")
# 写入多行
lines = ["第一行\n", "第二行\n", "第三行\n"]
with open("output.txt", "w", encoding="utf-8") as file:
file.writelines(lines)
使用os和pathlib模块处理文件路径。
import os
# 获取当前工作目录
print(os.getcwd())
# 改变工作目录
os.chdir("/path/to/directory")
# 列出目录内容
files = os.listdir(".")
print(files)
# 检查路径是否存在
if os.path.exists("data.txt"):
print("文件存在")
# 检查是否为文件或目录
print(os.path.isfile("data.txt")) # True
print(os.path.isdir("folder")) # True
# 获取文件大小
size = os.path.getsize("data.txt")
print(f"文件大小: {size}字节")
# 拼接路径
path = os.path.join("folder", "subfolder", "file.txt")
print(path)
from pathlib import Path
# 创建Path对象
p = Path("data.txt")
# 检查文件是否存在
if p.exists():
print("文件存在")
# 读取文件
content = p.read_text(encoding="utf-8")
# 写入文件
p.write_text("Hello, World!", encoding="utf-8")
# 获取文件信息
print(p.name) # 文件名
print(p.stem) # 文件名(不含扩展名)
print(p.suffix) # 扩展名
print(p.parent) # 父目录
# 遍历目录
folder = Path(".")
for file in folder.glob("*.txt"):
print(file)
import os
# 创建目录
os.mkdir("new_folder")
# 创建多级目录
os.makedirs("parent/child/grandchild")
# 删除空目录
os.rmdir("new_folder")
# 删除文件
os.remove("file.txt")
# 重命名文件或目录
os.rename("old_name.txt", "new_name.txt")
# 遍历目录树
for root, dirs, files in os.walk("."):
print(f"目录: {root}")
for file in files:
print(f" 文件: {file}")
import csv
# 读取CSV
with open("data.csv", "r", encoding="utf-8") as file:
reader = csv.reader(file)
for row in reader:
print(row)
# 使用字典读取CSV
with open("data.csv", "r", encoding="utf-8") as file:
reader = csv.DictReader(file)
for row in reader:
print(row["name"], row["age"])
# 写入CSV
data = [
["姓名", "年龄", "城市"],
["张三", "25", "北京"],
["李四", "30", "上海"]
]
with open("output.csv", "w", encoding="utf-8", newline="") as file:
writer = csv.writer(file)
writer.writerows(data)
import json
# 读取JSON
with open("data.json", "r", encoding="utf-8") as file:
data = json.load(file)
print(data)
# 写入JSON
data = {
"name": "张三",
"age": 25,
"skills": ["Python", "Java", "JavaScript"]
}
with open("output.json", "w", encoding="utf-8") as file:
json.dump(data, file, ensure_ascii=False, indent=4)
# JSON字符串转换
json_str = json.dumps(data, ensure_ascii=False)
data_dict = json.loads(json_str)
# 读取二进制文件
with open("image.jpg", "rb") as file:
data = file.read()
print(f"文件大小: {len(data)}字节")
# 写入二进制文件
with open("copy.jpg", "wb") as file:
file.write(data)
# 复制文件
import shutil
shutil.copy("source.txt", "destination.txt")
shutil.copytree("source_folder", "dest_folder")
# 大文件分块读取
def read_large_file(file_path, chunk_size=1024):
with open(file_path, "r", encoding="utf-8") as file:
while True:
chunk = file.read(chunk_size)
if not chunk:
break
yield chunk
for chunk in read_large_file("large_file.txt"):
process(chunk)
# 练习1:统计单词
def count_words(filename):
with open(filename, "r", encoding="utf-8") as file:
content = file.read()
words = content.split()
return len(words)
print(f"单词数: {count_words('data.txt')}")
# 练习2:复制文件
def copy_file(source, destination):
with open(source, "r", encoding="utf-8") as src:
content = src.read()
with open(destination, "w", encoding="utf-8") as dst:
dst.write(content)
copy_file("source.txt", "copy.txt")
# 练习3:学生信息管理
import json
def save_students(students, filename):
with open(filename, "w", encoding="utf-8") as f:
json.dump(students, f, ensure_ascii=False, indent=4)
def load_students(filename):
with open(filename, "r", encoding="utf-8") as f:
return json.load(f)
students = [
{"name": "张三", "age": 20, "grade": "A"},
{"name": "李四", "age": 21, "grade": "B"}
]
save_students(students, "students.json")
# 练习4:列出Python文件
from pathlib import Path
def list_python_files(directory):
path = Path(directory)
for file in path.rglob("*.py"):
print(file)
list_python_files(".")