python中shutil和shutil库的用法

yizhihongxing

一、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')

原文链接:https://www.cnblogs.com/xxpythonxx/p/17298705.html

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:python中shutil和shutil库的用法 - Python技术站

(0)
上一篇 2023年4月18日
下一篇 2023年4月18日

相关文章

  • 使用Python3中的gettext模块翻译Python源码以支持多语言

    使用Python3中的gettext模块可以轻松地将Python源码翻译成多种语言,实现国际化的目的。下面是使用gettext模块翻译Python源码的完整攻略: 创建翻译文件 首先,需要创建一个翻译文件 (.po 文件),该文件包含原始语言的翻译以及每个需要翻译的字符串。可以使用 gettext 工具根据 Python 源码生成翻译文件。 例如,假设我们要…

    python 2023年6月5日
    00
  • Python去除PDF水印的实现示例

    下面是针对Python去除PDF水印的实现示例的详细攻略。 1. 安装需要的Python库 在使用Python进行PDF处理之前,需要安装相关的Python库。通常我们使用pdfplumber库来处理PDF文件,可以使用以下命令进行安装: pip install pdfplumber 此外,使用pillow可进行图像处理等功能,也可以使用以下命令进行安装: …

    python 2023年6月3日
    00
  • python基础教程之基本内置数据类型介绍

    Python基础教程之基本内置数据类型介绍 Python是一门简单易学,却非常强大的编程语言。这篇文章将介绍Python中的基本内置数据类型:整数、浮点数、布尔值、字符串和列表。 整数 整数是Python中最基本的数据类型之一,用于表示整数值。整数可以进行各种基本的数学运算,例如加减乘除和幂次方。 以下是一个简单的整数示例: a = 30 b = 20 pr…

    python 2023年5月14日
    00
  • 分析运行中的 Python 进程详细解析

    分析运行中的 Python 进程详细解析 在进行 Python 程序开发时,会遇到各种问题,如程序运行缓慢、内存占用高等。这些问题往往与 Python 进程运行时的资源占用有关。本文将介绍如何分析运行中的 Python 进程,以便了解程序的运行情况,优化程序性能。 调用 Python 中的 psutil 模块 psutil 模块是 Python 中用于获取系…

    python 2023年6月3日
    00
  • python求前n个阶乘的和实例

    下面是详细讲解 “python求前n个阶乘的和实例” 的完整攻略。 目录 问题描述 解决方案 示例说明 示例一 示例二 问题描述 假设有一个数n,求前n个数的阶乘的和,即$1!+2!+3!+…+(n-1)!+n!$。 解决方案 我们可以使用for循环和递归两种方法来求解这个问题。 方法一:for循环 使用for循环,我们可以遍历1到n的每一个数,并求出它…

    python 2023年6月5日
    00
  • Python实现微信小程序自动操作工具

    Python实现微信小程序自动操作工具 本攻略将详细介绍如何使用Python实现微信小程序自动操作工具,方便开发者快速进行小程序的测试、批量操作等。 前置条件 熟悉Python编程语言; 了解微信小程序的基本操作和运行机制; 安装selenium、chromedriver和wxpy等Python库。 实现步骤 1. 安装selenium和chromedriv…

    python 2023年5月19日
    00
  • Python控制台输出时刷新当前行内容而不是输出新行的实现

    为了实现Python控制台输出时刷新当前行内容而不是输出新行,我们需要用到sys模块以及对应的stdout和flush方法。 具体步骤如下: 导入sys模块 首先,在Python文件或控制台中导入sys模块,以便使用相关方法。可以使用以下命令导入sys模块: import sys 使用stdout方法替换输出 将标准输出(一般指print函数输出)替换成sy…

    python 2023年6月3日
    00
  • ndarray数组的转置(transpose)和轴对换方式

    ndarray数组的转置是指将数组的维度重新排列,而轴对换是指根据指定的维度进行转置操作。在NumPy中,可以通过transpose和swapaxes方法进行转置和轴对换操作。 转置操作 转置操作可以使用ndarray的transpose方法进行,该方法可以接受一个由对应维度索引组成的tuple作为参数。如果不指定参数,transpose方法默认对所有轴进行…

    python 2023年6月5日
    00
合作推广
合作推广
分享本页
返回顶部