首页 > 编程语言 >Python shutil模块

Python shutil模块

时间:2023-06-24 17:24:01浏览次数:35  
标签:shutil Users tar wupeiqi Python 模块 import txt

Python shutil模块

高级的 文件、文件夹、压缩包 处理模块

将文件内容拷贝到另一个文件中,可以部分内容

shutil.copyfileobj(fsrc, fdst[, length])
  • 例子
import shutil

f1 = open("a.txt", encoding="utf-8")

f2 = open("b.txt", "w", encoding="utf-8")
shutil.copyfileobj(f1, f2)
  • 源代码
def copyfileobj(fsrc, fdst, length=16*1024):
"""copy data from file-like object fsrc to file-like object fdst"""
while 1:
buf = fsrc.read(length)
if not buf:
break
fdst.write(buf)  

拷贝文件

shutil.copyfile(src, dst)
  • 例子
import shutil

shutil.copyfile("a.txt", "b.txt")

仅拷贝权限。内容、组、用户均不变

shutil.copymode(src, dst)
  • 例子
import shutil

shutil.copymode("a.txt", "b.txt")

拷贝状态的信息,包括:mode bits, atime, mtime, flags

shutil.copystat(src, dst)
  • 例子
import shutil

shutil.copystat("a.txt", "b.txt")

拷贝文件和权限

shutil.copy(src, dst)
  • 例子
import shutil

shutil.copy("a.txt", "b.txt")

拷贝文件和状态信息

shutil.copy2(src, dst)
  • 例子
import shutil

shutil.copy2("a.txt", "b.txt")

递归的去拷贝文件

shutil.ignore_patterns(*patterns)
shutil.copytree(src, dst, symlinks=False, ignore=None)

例如:copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))
  • 例子
import shutil

shutil.copytree("a", "b")

递归的去删除文件

shutil.rmtree(path[, ignore_errors[, one rror]])
  • 例子
import shutil

shutil.rmtree(b")

递归的去移动文件

shutil.move(src, dst)
  • 例子
import shutil

shutil.move("a.txt", "b.txt")

创建压缩包并返回文件路径,例如:zip、tar

shutil.make_archive(base_name, format,...)

base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径,
如:www =>保存至当前路径
如:/Users/wupeiqi/www =>保存至/Users/wupeiqi/
format: 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
root_dir: 要压缩的文件夹路径(默认当前目录)
owner: 用户,默认当前用户
group: 组,默认当前组
logger: 用于记录日志,通常是logging.Logger对象
  • 例子
#将 /Users/wupeiqi/Downloads/test 下的文件打包放置当前程序目录

import shutil
ret = shutil.make_archive("wwwwwwwwww", 'gztar', root_dir='/Users/wupeiqi/Downloads/test')


#将 /Users/wupeiqi/Downloads/test 下的文件打包放置 /Users/wupeiqi/目录
import shutil
ret = shutil.make_archive("/Users/wupeiqi/wwwwwwwwww", 'gztar', root_dir='/Users/wupeiqi/Downloads/test')

shutil 对压缩包的处理是调用 ZipFile 和 TarFile 两个模块来进行的,详细:

  • zipfile 压缩解压
import zipfile

# 压缩
z = zipfile.ZipFile('laxi.zip', 'w')
z.write('a.log')
z.write('data.data')
z.close()

# 解压
z = zipfile.ZipFile('laxi.zip', 'r')
z.extractall()
z.close()
  • tarfile 压缩解压
import tarfile

# 压缩
tar = tarfile.open('your.tar','w')
tar.add('/Users/wupeiqi/PycharmProjects/bbs2.zip', arcname='bbs2.zip')
tar.add('/Users/wupeiqi/PycharmProjects/cmdb.zip', arcname='cmdb.zip')
tar.close()

# 解压
tar = tarfile.open('your.tar','r')
tar.extractall() # 可设置解压地址
tar.close()

标签:shutil,Users,tar,wupeiqi,Python,模块,import,txt
From: https://www.cnblogs.com/evescn/p/17501354.html

相关文章

  • 【python基础】文件-初识文件
    文本文件可存储的数据量是非常多的。每当需要分析或修改存储在文件中的信息时,首先就是读取文件到内存中,为此可以一次性读取文件的全部内容,也可以以每次一行的方式逐步读取。1.读取文件1.1读取整个文件要读取文件,需要一个包含几行文本的文件。下面首先来创建一个poems文本文件,,里......
  • Python os模块
    Pythonos模块os模块用于提供系统级别的操作os.getcwd()#获取当前工作目录,即当前python脚本工作的目录路径os.chdir("dirname")#改变当前脚本工作目录;相当于shell下cdos.curdir#返回当前目录:('.')os.pardir#获取当前目录......
  • Python sys模块
    Pythonsys模块sys模块用于提供对解释器相关的操作sys.argv#命令行参数List,第一个元素是程序本身路径sys.exit(n)#退出程序,正常退出时exit(0)sys.version#获取Python解释程序的版本信息sys.maxint......
  • Python hashlib模块
    Pythonhashlib模块hashlib模块用于加密相关的操作,代替了md5模块和sha模块主要提供SHA1,SHA224,SHA256,SHA384,SHA512,MD5算法#md5废弃importmd5hash=md5.new()hash.update('admin')printhash.hexdigest()#sha废弃importshahash=sha.new()ha......
  • 在 Python 中,类型属于对象,变量是没有类型的
    在Python中,类型属于对象,变量是没有类型的:━━━━━━━━━━━━━━━━━━━━━━━━━a=[1,2,3]a="w3cschool"以上代码中,[1,2,3]是list类型,"w3cschool"是string类型,而变量a是没有类型,她仅仅是一个对象的引用(一个指针),可以是list类型对象,也可以指向是stri......
  • Python 模块(转载)
    Python常用模块模块分类自定义模块开源模块内置模块自定义模块定义模块情景一情景二情景三导入模块Python之所以应用越来越广泛,在一定程度上也依赖于其为程序员提供了大量的模块以供使用,如果想要使用模块,则需要导入。导入模块有一下几种方法:importmodulefrom......
  • python: Treeview Control binding data using tkinter and ttkbootstrap GUI
     """StudentUI.py读文件类date2023-06-24edit:GeovinDu,geovindu,涂聚文ide:PyCharm2023.1python11"""importdatetimeimportsysimportosfromtkinterimportttkfromtkinterimport*fromtkinter.ttkimport*fromttk......
  • 若依微服务swagger如何不显示某个模块的接口文档?
    在若依微服务项目中,如果不想暴露某个模块的swagger的接口文档,需要怎么做?本文以ruoyi-gen模块进行举例说明。  默认情况下,可以看到这里包含了ruoyi-gen模块,我们要做的是,要将ruoyi-gen进行隐藏。最终的预期结果如下图所示,可以看见,下图中,是不包含ruoyi-gen这个模块的。那我们具体应......
  • 用python打开文件夹
    '''用python打开文件夹''''''https://blog.csdn.net/yqyangcyq/article/details/105677844'''os.system("explorer.exe%s"%str_dizh)#打开(可重复打开)目标文件夹os.startfile(str_dizh)#打开(并且屏幕上只存在一个/不会重复打开......
  • "ntoskrnl"代表"NT Operating System Kernel",它是Windows操作系统的主要核心模块之一,
    Windows内核是微软Windows操作系统的核心组件,它负责管理操作系统的核心功能和提供关键的系统服务。Windows内核是运行在计算机硬件上的软件,它与硬件交互并管理资源分配、进程管理、设备驱动程序和系统安全等任务。Windows内核采用了一种称为“混合内核”的设计,结合了传统的单内核......