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, οnerrοr=None) // 删除整个目录树, ignore_errors:是否忽略删除失败错误, οnerrοr=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()

python shutil_Python3 shutil(高级文件操作模块)相关推荐

  1. [06]python3 shutil高级文件操作模块

    1.shutil是shell utility的缩写 shutil.move直接从一个地方挪到另一个地方,而os.rename常常只能重命名,不能挪动位置. 功能是: >>>shuti ...

  2. Python3 shutil(高级文件操作 模块)

    原文: http://blog.csdn.net/Rozol/article/details/72672698 #!/usr/bin/env python # coding=utf-8 __autho ...

  3. python中shutil模块的用法-高级文件操作模块

    目录 前言 一.shutil是什么? 二.使用步骤 1. 安装shutil库 2. shutil库的使用 3. 复制函数 3.1 shutil.copy(src,dst) ==> 拷贝文件和权限 ...

  4. Python内置模块---高级文件操作模块

    高级文件操作模块 code: # ************************************************************* """ 高级 ...

  5. Python零基础速成班-第8讲-Python文件操作File IO、高级文件处理模块shutil、CSV、JSON、多线程基础

    Python零基础速成班-第8讲-Python文件操作File I&O.高级文件处理模块shutil.CSV.JSON.多线程基础 学习目标 文件操作File I/O 高级文件处理模块shut ...

  6. Python中的File(文件)操作

    Python中的File(文件)操作 针对磁盘中的文件的读写.文件I/O I 输入(input) O输出(Output) 文件操作步骤:1.打开文件 2.读写文件 3.关闭文件 写入文件的操作:(把大 ...

  7. Python os模块 -Python系统编程中的操作模块

    Python os模块 -Python系统编程中的操作模块 用途:处理(文件与目录)操作 以下为简单的常用操作 import os 导入os模块 help(os) 查看os模块帮助文档 os.name ...

  8. 详解Python中的File(文件)操作

    目录 Python中的File(文件)操作 写入文件的操作: 读取文件的操作: 一.文件操作相关函数 1. open() 打开文件 2. seek() 设置文件指针的位置 3. write() 写入内 ...

  9. windows定时任务python shutil_python- shutil 高级文件操作

    简介 shutil模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作.对单个文件的操作也可参见os模块. 拷贝文件 shutil.copyfile(src, ...

最新文章

  1. Android 中三种启用线程的方法
  2. 【转】usermod 添加用户多个附属组
  3. 西安电子科技大学计算机录取分数,2021年西安电子科技大学投档线及各省最低录取分数线统计表...
  4. css实现鼠标覆盖显示大图
  5. Python Flask 中的路由
  6. Screaming Frog SEO Spider for Mac(网络爬虫软件)v16.0
  7. Linux自学shell命令
  8. 巧用WiFi实现会员制营销
  9. 北航、商汤、UCSD 提出首个点云二值网络 BiPointNet(ICLR2021)
  10. 5000字 大数据时代读书笔记_《大数据时代》读后感 读书笔记
  11. FinTech普惠中国,技术创新智慧杭州
  12. PDF文件中如何插入页面?分享一个实用小妙招
  13. 犹他州计算机科学,犹他州大学计算机科学computer science专业排名第201~250名(2020THE泰晤士高等教育世界大学排名)...
  14. java程序调用百度Geocoding API逆地址解析通过经纬度查询位置
  15. “天涯博客”“江西福利彩票网”等网站被挂马
  16. mybatis使用truncate清空表
  17. redis命令之string类型incr/decr命令用法详情
  18. SNP/单核苷酸多态性分析
  19. PJzhang:关闭wps小广告和快速关闭445端口
  20. iFIERO -- (一) 宇宙大战 SPACE BATTLE — 新建场景SCENE、精灵节点、PARTICLE粒子及背景音乐

热门文章

  1. Java1.6下载地址
  2. esp32使用查表法utf8转gbk
  3. 一元线性预测模型(机器学习篇)
  4. 【完美解决】RuntimeError: one of the variables needed for gradient computation has been modified by an inp
  5. ubuntu 12.04中超强两笔输入法的安装与配置
  6. 英语范文:申请加薪范文
  7. 离散元仿真-PFC,3DEC
  8. Conmi的正确答案——Centos安装ufw
  9. 电商促销都是套路,长盛不衰的零售之道在哪儿?
  10. Eclipse新建jsp页面时,第一行会报错的解决方式