网站首页 > 技术文章 正文
shutil 是 Python 的标准库之一,提供了许多文件和目录操作的功能。以下是 shutil 常用的功能列表,并给出了每个功能的简单示例:
文件和目录的复制
- (shutil.copy, shutil.copy2, shutil.copyfile, shutil.copytree):
import shutil
# 复制文件
shutil.copy('source.txt', 'destination.txt')
# 复制文件,保留元数据(如访问时间、修改时间等)
shutil.copy2('source.txt', 'destination.txt')
# 复制文件内容(不包括元数据)
shutil.copyfile('source.txt', 'destination.txt')
# 复制目录及其内容
shutil.copytree('source_dir', 'destination_dir')
文件和目录的移动和重命名 (shutil.move, shutil.rename):
import shutil
# 移动文件或目录
shutil.move('source.txt', 'destination.txt')
# 重命名文件或目录
shutil.rename('old_name.txt', 'new_name.txt')
文件和目录的删除 (shutil.rmtree, os.remove):
import shutil
# 递归删除目录及其内容
shutil.rmtree('directory')
# 删除文件
os.remove('file.txt')
压缩和解压缩文件 (shutil.make_archive, shutil.unpack_archive):
import shutil
# 创建压缩文件(支持多种格式,如 zip、tar、gztar)
shutil.make_archive('archive', 'zip', 'directory')
# 解压缩压缩文件
shutil.unpack_archive('archive.zip', 'extracted_directory')
文件和目录的权限设置 (shutil.chown, shutil.chmod):
import shutil
# 修改文件或目录的所有者和组
shutil.chown('file.txt', user='new_user', group='new_group')
# 修改文件或目录的权限
shutil.chmod('file.txt', mode=0o755)
获取文件大小、获取最后访问时间等 (shutil.disk_usage, shutil.getatime, shutil.getmtime, shutil.getctime):
import shutil
# 获取磁盘使用情况
usage = shutil.disk_usage('/path/to/directory')
print(usage.total, usage.used, usage.free)
# 获取文件或目录的最后访问时间、修改时间、创建时间
atime = shutil.getatime('file.txt')
mtime = shutil.getmtime('file.txt')
ctime = shutil.getctime('file.txt')
目录的遍历 (shutil.walk):
import shutil
# 遍历目录及其子目录中的所有文件和文件夹
for root, dirs, files in shutil.walk('directory'):
print(root) # 当前遍历的目录
print(dirs) # 当前目录下的子目录
print(files) # 当前目录下的文件
文件和目录的比较 (shutil.cmp):
import shutil
# 比较两个文件的内容是否相同
same = shutil.cmp('file1.txt', 'file2.txt')
# 比较两个目录的内容是否相同
same = shutil.cmp('dir1', 'dir2')
创建临时文件和目录 (shutil.mkstemp, shutil.mkdtemp):
import shutil
# 创建临时文件(返回文件对象和文件路径)
temp_file = shutil.mkstemp(suffix='.txt', prefix='temp_', dir='temp_dir')
# 创建临时目录(返回目录路径)
temp_dir = shutil.mkdtemp(prefix='temp_')
获取文件或目录的状态信息 (shutil.stat):
import shutil
# 获取文件或目录的状态信息
stat_info = shutil.stat('file.txt')
# 获取文件或目录的大小(字节数)
file_size = stat_info.st_size
# 获取文件或目录的权限(八进制数表示)
file_mode = stat_info.st_mode
# 获取文件或目录的最后访问时间
access_time = stat_info.st_atime
猜你喜欢
- 2025-03-17 一文掌握在 Python 中保存和加载 JSON 文件
- 2025-03-17 轻松玩转Python文件操作:移动、删除
- 2025-03-17 Python包中__init__.py文件的作用和用法
- 2025-03-17 Python 文件处理指南(python文件的操作步骤)
- 2025-03-17 python基础—pip指定包安装目录(pythonpip安装的默认路径)
- 2025-03-17 Python小案例55- os模块执行文件路径
- 2025-03-17 Python写入Excel表格模块:xlwt介绍
- 2025-03-17 Python OS 库基础知识(os.python)
- 2025-03-17 Python 文件操作:从基础到实践(python如何进行文件操作)
- 2025-03-17 Python 模块详解教程(python模块总结)
- 05-25Python 3.14 t-string 要来了,它与 f-string 有何不同?
- 05-25Python基础元素语法总结
- 05-25Python中的变量是什么东西?
- 05-25新手常见的python报错及解决方案
- 05-2511-Python变量
- 05-2510个每个人都是需要知道Python问题
- 05-25Python编程:轻松掌握函数定义、类型及其参数传递方式
- 05-25Python基础语法
- 257℃Python短文,Python中的嵌套条件语句(六)
- 257℃python笔记:for循环嵌套。end=""的作用,图形打印
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 91℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 81℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- 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)