网站首页 > 技术文章 正文
shutil是Python内置的模块,提供了高级文件操作功能,支持复制、移动、删除、压缩、解压缩等操作。
- 复制文件到目标目录
import shutil
src_file = 'path/to/source/file'
dst_dir = 'path/to/destination/dir'
shutil.copy(src_file, dst_dir)
- 复制文件并重命名
import shutil
src_file = 'path/to/source/file'
dst_dir = 'path/to/destination/dir/new_name.txt'
shutil.copy(src_file, dst_dir)
- 复制整个目录
import shutil
src_dir = 'path/to/source/dir'
dst_dir = 'path/to/destination/dir'
shutil.copytree(src_dir, dst_dir)
- 移动文件到目标目录
import shutil
src_file = 'path/to/source/file'
dst_dir = 'path/to/destination/dir'
shutil.move(src_file, dst_dir)
- 移动文件并重命名
import shutil
import os
src_file = 'path/to/source/file'
dst_dir = 'path/to/destination/dir/new_name.txt'
if os.path.isfile(dst_dir):
os.remove(dst_dir)
shutil.move(src_file, dst_dir)
- 移动整个目录
import shutil
src_dir = 'path/to/source/dir'
dst_dir = 'path/to/destination/dir'
shutil.move(src_dir, dst_dir)
- 删除文件
import os
file_path = 'path/to/file'
os.remove(file_path)
- 删除目录
import shutil
dir_path = 'path/to/dir'
shutil.rmtree(dir_path)
- 压缩单个文件
import shutil
file_path = 'path/to/file'
zip_path = 'path/to/archive.zip'
shutil.make_archive(zip_path, 'zip', file_path)
- 压缩整个目录
import shutil
dir_path = 'path/to/dir'
zip_path = 'path/to/archive.zip'
shutil.make_archive(zip_path, 'zip', dir_path)
- 解压缩zip文件
import shutil
zip_path = 'path/to/archive.zip'
dst_path = 'path/to/destination/dir'
shutil.unpack_archive(zip_path, dst_path)
- 文件比较
import filecmp
file_path1 = 'path/to/file1'
file_path2 = 'path/to/file2'
result = filecmp.cmp(file_path1, file_path2)
if result:
print('文件一致')
else:
print('文件不一致')
- 文件差异比较
import difflib
file_path1 = 'path/to/file1'
file_path2 = 'path/to/file2'
with open(file_path1) as f1:
with open(file_path2) as f2:
diff = difflib.unified_diff(f1.readlines(), f2.readlines())
for line in diff:
print(line)
- 目录比较
import filecmp
dir_path1 = 'path/to/dir1'
dir_path2 = 'path/to/dir2'
dcmp = filecmp.dircmp(dir_path1, dir_path2)
print('只存在一个目录中的文件:')
print(dcmp.left_only)
print(dcmp.right_only)
print('不同内容的文件:')
print(dcmp.diff_files)
print('相同内容但不同名的文件:')
print(dcmp.common_crc)
print('子目录的差异:')
print(dcmp.subdirs)
- 拷贝整个目录并排除某些文件或目录
import shutil
src_dir = 'path/to/src/dir'
dst_dir = 'path/to/dst/dir'
exclude_files = ['*.log', '*.out']
exclude_dirs = ['temp']
shutil.copytree(src_dir, dst_dir, ignore=shutil.ignore_patterns(*exclude_files, *exclude_dirs))
- 移动某个目录并排除某些文件或目录
import shutil
src_dir = 'path/to/src/dir'
dst_dir = 'path/to/dst/dir'
exclude_files = ['*.log', '*.out']
exclude_dirs = ['temp']
shutil.move(src_dir, dst_dir, ignore=shutil.ignore_patterns(*exclude_files, *exclude_dirs))
- 递归复制文件夹并覆盖
import shutil
src_dir = 'path/to/src/dir'
dst_dir = 'path/to/dst/dir'
shutil.rmtree(dst_dir)
shutil.copytree(src_dir, dst_dir)
- 拷贝单个目录所有内容到另一个目录下的指定子目录中
import shutil
src_dir = 'path/to/src/dir'
dst_dir = 'path/to/dst/dir'
sub_dir = 'subdir'
shutil.copytree(src_dir, os.path.join(dst_dir, sub_dir))
- 复制目录但是不复制空目录
import os
import shutil
src_dir = 'path/to/src/dir'
dst_dir = 'path/to/dst/dir'
for root, dirs, files in os.walk(src_dir):
for file in files:
src_file = os.path.join(root, file)
rel_path = os.path.relpath(src_file, src_dir)
dst_file = os.path.join(dst_dir, rel_path)
if not os.path.exists(os.path.dirname(dst_file)):
os.makedirs(os.path.dirname(dst_file))
shutil.copy2(src_file, dst_file)
注意事项
- shutil操作会直接影响源文件,应该谨慎使用。
- 使用shutil.copytree()复制目录时需要注意目标目录必须不存在,否则会抛出异常。
- 可以使用shutil.ignore_patterns()或shutil.ignore_function()来忽略某些文件或文件夹。
、
猜你喜欢
- 2024-11-27 「重磅」Xilinx下载文件破解及LUT在线可编程研究
- 2024-11-27 2020年4月Redis面试题和答案整理
- 2024-11-27 分析Redis key,value的size
- 2024-11-27 自主可控的PLC编程软件:kVPAC/Beremiz操作实践
- 2024-11-27 面试官:说说 Redis 的缓存雪崩、缓存穿透和缓存击穿问题
- 2024-11-27 Redis内存管理:配置与版本事项
- 2024-11-27 Kubernetes全栈架构师(Docker基础)--学习笔记
- 2024-11-27 串口通讯继电器-modbus通信上位机调试软件工具项目开发案例
- 2024-11-27 GCKontrol模型的自动创建
- 2024-11-27 推荐:工业数字化系统开发用到的串口调试小助手
- 1507℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 505℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 485℃MySQL service启动脚本浅析(r12笔记第59天)
- 465℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 462℃启用MySQL查询缓存(mysql8.0查询缓存)
- 443℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 422℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 418℃MySQL server PID file could not be found!失败
- 最近发表
-
- netty系列之:搭建HTTP上传文件服务器
- 让deepseek教我将deepseek接入word
- 前端大文件分片上传断点续传(前端大文件分片上传断点续传怎么操作)
- POST 为什么会发送两次请求?(post+为什么会发送两次请求?怎么回答)
- Jmeter之HTTP请求与响应(jmeter运行http请求没反应)
- WAF-Bypass之SQL注入绕过思路总结
- 用户疯狂点击上传按钮,如何确保只有一个上传任务在执行?
- 二 计算机网络 前端学习 物理层 链路层 网络层 传输层 应用层 HTTP
- HTTP请求的完全过程(http请求的基本过程)
- dart系列之:浏览器中的舞者,用dart发送HTTP请求
- 标签列表
-
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- chromepost (65)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- js判断是否是json字符串 (67)
- checkout-b (67)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- linux删除一个文件夹 (65)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)