python shutil 模块

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

shutil.copyfileobj(fsrc, fdst[, length])(copyfileobj方法只会拷贝文件内容)
将文件内容拷贝到另一个文件中

import shutil

shutil.copyfileobj(open('old.xml','r'), open('new.xml', 'w'))

shutil.copyfile(src, dst)  (copyfile只拷贝文件内容)
拷贝文件

shutil.copyfile('f1.log', 'f2.log')

shutil.copy(src, dst) 拷贝文件和权限

shutil.copy('f1.log', 'f2.log')

shutil.copy2(src, dst)

拷贝文件和状态信息

shutil.copy2('f1.log', 'f2.log'

 

shutil.copymode(src, dst)  (前提是dst文件存在,不然报错)
仅拷贝权限。内容、组、用户均不变

shutil.copymode('f1.log', 'f2.log')

shutil.copystat(src, dst)
仅拷贝状态的信息,即文件属性,包括:mode bits, atime, mtime, flags

shutil.copystat('f1.log', 'f2.log')

 

shutil.ignore_patterns(*patterns)  (忽略哪个文件,有选择性的拷贝)

shutil.copytree(src, dst, symlinks=False, ignore=None)
递归的去拷贝文件夹

shutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))
shutil.copytree('f1', 'f2', symlinks=True, ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))

shutil.rmtree(path[, ignore_errors[, onerror]])
递归的去删除文件 

shutil.rmtree('folder1')

删除单个文件 用os.remove(filename); 或者  用#os.unlink(my_file)

shutil.move(src, dst)

递归的去移动文件,它类似mv命令,其实就是重命名。
shutil.move('folder1', 'folder3')

 

 

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

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

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

  • 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 两个模块来进行的,详细:

复制代码
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()
复制代码
复制代码
import tarfile

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

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


备注:zipfile压缩不会保留文件的状态信息,而tarfile会保留文件的状态信息


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`shutil` 模块Python 中用于对文件和文件夹进行操作的标准库之一。它提供了一些简单而又强大的函数,使得文件和文件夹的复制、移动、删除等操作变得非常容易。 下面是一些常见的 `shutil` 模块中的函数及其用法: 1. `shutil.copy(src, dst)`:将文件从源路径复制到目标路径。如果目标路径是一个文件,则覆盖该文件;如果目标路径是一个目录,则在该目录下创建同名文件并复制。 ```python import shutil src_path = "source_file.txt" dst_path = "destination_file.txt" shutil.copy(src_path, dst_path) ``` 2. `shutil.copy2(src, dst)`:与 `shutil.copy()` 类似,但还会复制文件的元数据(如创建时间、修改时间等)。 ```python import shutil src_path = "source_file.txt" dst_path = "destination_file.txt" shutil.copy2(src_path, dst_path) ``` 3. `shutil.move(src, dst)`:将文件或目录从源路径移动到目标路径。如果目标路径是一个文件,则覆盖该文件;如果目标路径是一个目录,则在该目录下创建同名文件或目录并移动。 ```python import shutil src_path = "source_file.txt" dst_path = "destination_file.txt" shutil.move(src_path, dst_path) ``` 4. `shutil.rmtree(path)`:递归删除一个目录及其内容。 ```python import shutil dir_path = "directory_to_delete" shutil.rmtree(dir_path) ``` 5. `shutil.make_archive(base_name, format, root_dir)`:创建一个压缩包,并返回压缩包的文件路径。`base_name` 是压缩包的文件名,`format` 是压缩包的格式(如 "zip"、"tar" 等),`root_dir` 是要打包的根目录。 ```python import shutil dir_to_archive = "directory_to_archive" archive_name = "archive" archive_format = "zip" shutil.make_archive(archive_name, archive_format, dir_to_archive) ``` 以上是 `shutil` 模块中常用的一些函数,更多函数可以查看 Python 官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值