python中shutil和shutil库的用法
最编程
2024-01-17 20:55:09
...
一、shutil目录和文件操作
Python shutil库提供了对文件和目录复制、移动、删除、压缩、解压等操作。
1. 复制文件或目录
- shutil.copy(src, dst):复制文件或目录
- shutil.copyfile(src, dst):复制文件,src和dst只能是文件
- shutil.copytree(src, dst, dirs_exist_ok=False):复制目录,默认dst目录不存在,否则会报错。
示例:
import os
import shutil
dirpath = os.path.dirname(os.path.realpath(__file__))
sourcedir = os.path.join(dirpath, "shutil_a")
sourcefile = os.path.join(dirpath, "shutil_a", "test.txt")
destdir = os.path.join(dirpath, "shutil_b")
destfile = os.path.join(dirpath, "shutil_b", "test2.txt")
# 复制文件或目录
shutil.copy(sourcefile, destdir)
# 复制文件
shutil.copyfile(sourcefile, destfile)
# 复制目录
shutil.copytree(sourcedir, destfile, dirs_exist_ok=True)
2. 移动文件或目录
语法:shutil.move(src, dst)
示例:
import os
import shutil
dirpath = os.path.dirname(os.path.realpath(__file__))
sourcedir = os.path.join(dirpath, "shutil_a")
sourcefile = os.path.join(dirpath, "shutil_a", "test.txt")
destdir = os.path.join(dirpath, "shutil_b")
shutil.move(sourcefile, destdir)
shutil.move(destdir, sourcedir)
3. 删除文件和目录
删除某个文件使用 os 模块提供的remove和unlink方法:
- os.remove(path)
- os.unlink(path)
删除目录使用 shutil.rmtree 方法:
import os
import shutil
dirpath = os.path.dirname(os.path.realpath(__file__))
destdir = os.path.join(dirpath, "shutil_b")
shutil.rmtree(destdir)
二、shutil文件压缩、解压
shutil库也支持文件压缩、解压操作,这个功能在Python 3.2版本引入。
1. 压缩文件
语法格式:
shutil.make_archive(base_name, format[, root_dir[, base_dir[, verbose[, dry_run[, owner[, group[, logger]]]]]]])
- base_name:压缩包文件名
- format:压缩包格式,支持zip,tar,bztar,gztar,xztar格式,可使用shutil.get_archive_formats()方法查看
- root_dir:要压缩文件路径的根目录(默认当前目录)
- base_dir:相对于root_dir的压缩文件路径(默认当前目录)
示例:
import os
import shutil
#Python小白学习交流群:725638078
dirpath = os.path.dirname(os.path.realpath(__file__))
archive_name = os.path.join(dirpath, "shutil_a")
root_dir = archive_name
shutil.make_archive(archive_name, 'zip', root_dir)
2. 解压文件
语法格式:
shutil.unpack_archive(filename[, extract_dir[, format]])
示例:
import os
import shutil
dirpath = os.path.dirname(os.path.realpath(__file__))
archive_name = os.path.join(dirpath, "shutil_a.zip")
extract_dir = os.path.join(dirpath, "shutil_a")
shutil.unpack_archive(archive_name, extract_dir, 'zip')
推荐阅读
-
2 Python 中的常用数据可视化库:Bokeh 和 Altair
-
python 正弦拟合_Python 中的正弦余弦曲线拟合_Python 正弦拟合基础和 LMFIT 库 - 正弦拟合之和
-
概率论中的常见分布摘要和 python scipy 库的使用:两点分布、二项分布、几何分布、泊松分布、均匀分布、指数分布和正态分布
-
[姿势估计] 实践记录:使用 Dlib 和 mediapipe 进行人脸姿势估计 - 本文重点介绍方法 2):方法 1:基于深度学习的方法:。 基于深度学习的方法:基于深度学习的方法利用深度学习模型,如卷积神经网络(CNN)或递归神经网络(RNN),直接从人脸图像中学习姿势估计。这些方法能够学习更复杂的特征表征,并在大规模数据集上取得优异的性能。方法二:基于二维校准信息估计三维姿态信息(计算机视觉 PnP 问题)。 特征点定位:人脸姿态估计的第一步是通过特征点定位来检测和定位人脸的关键点,如眼睛、鼻子和嘴巴。这些关键点提供了人脸的局部结构信息,可用于后续的姿势估计。 旋转表示:常见的旋转表示方法包括欧拉角和旋转矩阵。欧拉角通过三个旋转角度(通常是俯仰、偏航和滚动)描述头部的旋转姿态。旋转矩阵是一个 3x3 矩阵,表示头部从一个坐标系到另一个坐标系的变换。 三维模型重建:根据特征点的定位结果,三维人脸模型可用于姿势估计。通过将人脸的二维图像映射到三维模型上,可以估算出人脸的旋转和平移信息。这就需要建立人脸的三维模型,然后通过优化方法将模型与特征点对齐,从而获得姿势估计结果。 特征点定位 特征点定位是用于检测人脸关键部位的五官基础部分,还有其他更多的特征点表示方法,大家可以参考我上一篇文章中介绍的特征点检测方案实践:人脸校正二次定位操作来解决人脸校正的问题,客户在检测关键点的代码上略有修改,坐标转换部分客户见上图 def get_face_info(image). img_copy = image.copy image.flags.writeable = False image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) results = face_detection.process(image) # 在图像上绘制人脸检测注释。 image.flags.writeable = True image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) box_info, facial = None, None if results.detections: for detection in results. for detection in results.detections: mp_drawing.Drawing.detection = 无 mp_drawing.draw_detection(image, detection) 面部 = detection.location_data.relative_keypoints 返回面部 在上述代码中,返回的数据是五官(6 个关键点的坐标),这是用 mediapipe 库实现的,下面我们可以尝试用另一个库:dlib 来实现。 使用 dlib 使用 Dlib 库在 Python 中实现人脸关键点检测的步骤如下: 确保已安装 Dlib 库,可使用以下命令: pip install dlib 导入必要的库: 加载 Dlib 的人脸检测器和关键点检测器模型: 读取图像并将其灰度化: 使用人脸检测器检测图像中的人脸: 对检测到的人脸进行遍历,并使用关键点检测器检测人脸关键点: 显示绘制了关键点的图像: 以下代码将参数 landmarks_part 添加到要返回的关键点坐标中。
-
tinytag--用于从 MP3、OGG、FLAC 和 Wave 文件中读取音乐元数据的 python 库。
-
python hashlib 库(MD5、sha1、sha256、sha512、pbkdf2_hmac)的用法和 pbkdf2 原理
-
[Python 中 -m 的典型用法、原理和演变
-
line.split 在 python 中的用法和实际使用示例
-
Python的qbittorrent库:电影下载学习笔记(包括基础用法和常见函数)
-
python中shutil和shutil库的用法