Python3 shutil(高级文件操作模块)实例用法总结

 更新时间:2020年02月19日 16:53:50   作者:周小董  
在本篇文章里小编给大家整理的是一篇关于Python3 shutil实例用法内容,有兴趣的朋友们可以学习下。

1、shutil是shell

utility的缩写

shutil.move直接从一个地方挪到另一个地方,而os.rename常常只能重命名,不能挪动位置。

功能是:

>>>shutil.move('old.txt',r'c:datarchive')
>>>shutil.copy('old.txt',r'c:datarchive')
>>>os.remove('junk.dat')

2、高级文件操作(拷贝/移动/压缩/解压缩)

#!/usr/bin/env python
# coding=utf-8
__author__ = 'zhuo'
__date__ = '2017/5/25'
# shutil_demo.py 高级文件操作(拷贝 / 移动 / 压缩 / 解压缩)

import shutil


def shutil_demo():
  # 拷贝文件
  shutil.copy2('file.txt', 'temp.txt')

  # 拷贝目录
  shutil.copytree("root", "temp", symlinks=False, ignore=shutil.ignore_patterns("*.pyc"), copy_function=shutil.copy2, ignore_dangling_symlinks=True)

  # 删除目录
  shutil.rmtree("temp", ignore_errors=True)

  # 移动文件/目录
  shutil.move("root", "temp", copy_function=shutil.copy2)

  # 获取磁盘使用空间
  total, used, free = shutil.disk_usage(".")
  print("当前磁盘共: %iGB, 已使用: %iGB, 剩余: %iGB"%(total / 1073741824, used / 1073741824, free / 1073741824))

  # 压缩文件
  shutil.make_archive('Box', 'zip', 'temp')

  # 解压文件
  shutil.unpack_archive('Box.zip')



def shutil_func():
  # 文件和目录操作
  # shutil.copyfileobj(fsrc, fdst[, length]) // 拷贝文件内容, 将fsrc文件里的内容copy到fdst文件中, length:缓冲区大小
  shutil.copyfileobj(open('file.txt', 'r'), open('temp.txt', 'w'))
  # shutil.copyfile(src, dst, *, follow_symlinks=True) // 拷贝文件内容, 同copyfileobj, 如果dst=src,抛SameFileError异常, dst存在则替换
  dst = shutil.copyfile('file.txt', 'temp.txt')
  # shutil.copymode(src, dst, *, follow_symlinks=True) // 仅拷贝权限, 其他信息不受影响
  shutil.copymode('file.txt', 'temp.txt')
  # shutil.copystat(src, dst, *, follow_symlinks=True) // 拷贝状态(权限 / 最后访问时间 / 上次修改时间 / 标志), 其他不受迎影响
  shutil.copystat('file.txt', 'temp.txt')
  # shutil.copy(src, dst, *, follow_symlinks=True) // 拷贝文件(数据 / 权限)
  dst = shutil.copy('file.txt', 'temp.txt')
  # shutil.copy2(src, dst, *, follow_symlinks=True) // 拷贝文件(尝试保留所有元数据) (不能拷贝创建时间,该时间可通过修改系统时间再创建文件来实现)
  dst = shutil.copy2('file.txt', 'temp.txt')
  # shutil.ignore_patterns(*patterns)
  # symlinks:True(复制链接) / False(复制文件), ignore=ignore_patterns("") // 忽略的文件, copy_function=自定义复制函数, ignore_dangling_symlinks:True(忽略文件不存在异常) / False(错误列表中添加异常)
  # shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False) // 递归的复制根目录下的整个目录树
  dst = shutil.copytree("root", "temp", symlinks=False, ignore=shutil.ignore_patterns("*.pyc"), copy_function=shutil.copy2, ignore_dangling_symlinks=True)
  # shutil.rmtree(path, ignore_errors=False, onerror=None) // 删除整个目录树, ignore_errors:是否忽略删除失败错误, onerror=def error(func, path, excinfo)
  shutil.rmtree("temp", ignore_errors=True)
  # shutil.move(src, dst, copy_function=copy2) // 递归移动文件/目录, 目录存在则移动目录, 文件存在则覆盖
  dst = shutil.move("root", "temp", copy_function=shutil.copy2)
  total, used, free = shutil.disk_usage(".") # 给定路径的磁盘使用情况统计信息
  # shutil.chown(path, user=None, group=None) // 修改用户和组 (Unix可用)
  # shutil.which(cmd, mode=os.F_OK | os.X_OK, path=None) // 可执行文件路径, path:要查找的路径,未指定使用os.environ的结果
  path_str = shutil.which("python")


  # 异常
  try: pass
  except shutil.SameFileError: pass # copyfile()时,源和目录是同一个文件时,抛此异常
  except shutil.Error: pass # copytree()时, 多文件操作时引发的异常, 异常包含(srcname, dstname, excinfo)



  # 压缩文件操作 (封装了zipfile / tarfile)
  # 创建归档文件 base_name:压缩包文件名, format:格式 zip / tar / bztar / xztar / gztar, root_dir:被归档的根目录(默认当前目录)
  # base_dir:保存归档文件的目录(默认当前目录) verbose:已弃用 dry_run:True(不创建归档,但记录日志), owner:用户, group:用户组, logger:日志
  # shutil.make_archive(base_name, format[, root_dir[, base_dir[, verbose[, dry_run[, owner[, group[, logger]]]]]]])
  dst = shutil.make_archive('Box', 'zip', 'temp') # 注意:root_dir / base_dir至少写一个,不然会造成压缩包再次被打包的情况
  # 分拆归档, filename:文件名, extract_dir:解压到目录(默认当前目录), format:格式 (未提供,根据扩展名查找,未找到引发ValueError)
  # shutil.unpack_archive(filename[, extract_dir[, format]])
  shutil.unpack_archive('Box.zip')

  lists = shutil.get_archive_formats() # 返回支持的归档格式列表[(format, info)]
  lists = shutil.get_unpack_formats() # 返回所有注册格式的列表[(name, extensions, description)]

  # 注册压缩格式, name:格式名, function:def func(base_name, base_dir, owner, group, dry_run, logger), extra_args:额外参数, description:说明信息
  # shutil.register_archive_format(name, function[, extra_args[, description]])
  # shutil.unregister_archive_format(name) // 注销压缩格式
  # 注册解压格式 name:格式名, extensions:扩展名列表, function:实现函数 def unpack(filename, extract_dir), extra_args:额外参数(name, value), description:说明
  # shutil.register_unpack_format(name, extensions, function[, extra_args[, description]])
  # shutil.unregister_unpack_format(name) // 注销解压格式



  # 终端
  # shutil.get_terminal_size(fallback=(columns, lines))
  columns, lines = shutil.get_terminal_size() # 查询终端大小(宽, 高), 无法查询返回默认大小(80, 24)



if __name__ == "__main__":
  shutil_demo()

  # shutil_func()
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

知识点补充

shutil 模块

shutil可以简单地理解为sh + util,shell工具的意思。shutil模块是对os模块的补充,主要针对文件的拷贝、删除、移动、压缩和解压操作。

拷贝文件, shutil会自动识别拷贝的到底是文件还是文件夹, 如果存在同名的文件将会自动进行覆盖。

以上就是脚本之家小编给大家整理的全部相关知识点,感谢大家的学习和支持。

相关文章

  • Python解释器及PyCharm工具安装过程

    Python解释器及PyCharm工具安装过程

    这篇文章主要介绍了Python解释器和python 开发工具PyCharm安装过程,本文通过图文并茂的形式给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-02-02
  • 超详细注释之OpenCV dlib实现人脸采集

    超详细注释之OpenCV dlib实现人脸采集

    这篇文章主要介绍了OpenCV dlib实现人脸采集,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-09-09
  • python的移位操作实现详解

    python的移位操作实现详解

    这篇文章主要介绍了ppython的移位操作实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • Python中实现定时任务常见的几种方式

    Python中实现定时任务常见的几种方式

    在Python中,实现定时任务是一个常见的需求,无论是在自动化脚本、数据处理、系统监控还是其他许多应用场景中,Python提供了多种方法来实现定时任务,包括使用标准库、第三方库以及系统级别的工具,本文将详细介绍几种常见的Python定时任务实现方式
    2024-08-08
  • python使用js2py库运行js代码

    python使用js2py库运行js代码

    本文主要介绍了thon使用js2py库运行js代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-05-05
  • pd.DataFrame统计各列数值多少的实例

    pd.DataFrame统计各列数值多少的实例

    今天小编就为大家分享一篇pd.DataFrame统计各列数值多少的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12
  • Python接入MySQL实现增删改查的实战记录

    Python接入MySQL实现增删改查的实战记录

    这篇文章主要给大家介绍了关于Python接入MySQL实现增删改查的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • Python迭代器和生成器介绍

    Python迭代器和生成器介绍

    这篇文章主要介绍了Python迭代器和生成器介绍,本文分别用代码实例讲解了Python的迭代器和生成器,需要的朋友可以参考下
    2015-03-03
  • python 实现字符串下标的输出功能

    python 实现字符串下标的输出功能

    这篇文章主要介绍了python 简单的实现字符串下标的输出,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-02-02
  • jupyter notebook参数化运行python方式

    jupyter notebook参数化运行python方式

    这篇文章主要介绍了jupyter notebook参数化运行python方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04

最新评论