Python shutil模块用法实例分析
Python  /  管理员 发布于 7年前   223
本文实例讲述了Python shutil模块用法。分享给大家供大家参考,具体如下:
主要作用与拷贝文件用的。
1.shutil.copyfileobj(文件1,文件2):将文件1的数据覆盖copy给文件2。
import shutilf1 = open("1.txt",encoding="utf-8")f2 = open("2.txt","w",encoding="utf-8")shutil.copyfileobj(f1,f2)
2.shutil.copyfile(文件1,文件2):不用打开文件,直接用文件名进行覆盖copy。
import shutilshutil.copyfile("1.txt","3.txt")
3.shutil.copymode(文件1,文件2):之拷贝权限,内容组,用户,均不变。
def copymode(src,dst): """copy mode bits from src to dst""" if hasattr(os,'chmod'): st = os.stat(stc) mode = stat.S_IMODE(st.st_mode) os.chmod(dst,mode)
4.shutil.copystat(文件1,文件):只拷贝了权限。
def copystat(src,dst): """将所有的状态信息(模式位、时间、时间、标志)从src复制到dst""" st = os.stat(src) mode = stat.S_IMODE(st.st_mode) if hasattr(os, 'utime'): os.utime(dst,(st.st_atime,st.st_mtime)) if hasattr(os, 'chmod') os.chmod(dst,mode) if hasattr(os, 'chflags') and hasattr(st,'st_flags'): try: os.chflags(dst, st.st_flags) except OSError,why: for err in 'EOPNOTSUPP', 'ENOTSUP': if hasattr(errno,err) and why.errno == getattr(errno, err): break else: raise
5.shutil.copy(文件1,文件2):拷贝文件和权限都进行copy。
def copy(src,dst): """copy data and mode bits ("cp src dst") The destination may be a directory. """ if os.path.isdir(dst): dst = os.path.join(dst,os.path.basename(src)) copyfile(src,dst) copymode(src,dst)
6.shutil.copy2(文件1,文件2):拷贝了文件和状态信息。
7.shutil.copytree(源目录,目标目录):可以递归copy多个目录到指定目录下。
递归的去拷贝文件
例如:copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))
8.shutil.rmtree(目标目录):可以递归删除目录下的目录及文件。
9.shutil.move(源文件,指定路径):递归移动一个文件。
10.shutil.make_archive():可以压缩,打包文件。
import shutilshutil.make_archive("shutil_archive_test","zip","D:\新建文件夹 (2)")
11.shutil.make_archive(base_name, format,...)
创建压缩包并返回文件路径,例如:zip、tar
#将 /Users/wupeiqi/Downloads/test 下的文件打包放置当前程序目录import shutilret = shutil.make_archive("wwwwwwwwww", 'gztar', root_dir='/Users/wupeiqi/Downloads/test')#将 /Users/wupeiqi/Downloads/test 下的文件打包放置 /Users/wupeiqi/目录import shutilret = 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()
第二种方法:
import zipfilez = zipfile.ZipFile("day5.zip","w")z.write("a")
解压:
z.extractall("a")
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python文件与目录操作技巧汇总》、《Python文本文件操作技巧汇总》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号