zoukankan      html  css  js  c++  java
  • Python对文件的解压和压缩

    zipfile:

    解压:
    import os, zipfile
    serverzip_path = 'D:\server.zip'
    serverzip_target_path = 'd:\2'
    f = zipfile.ZipFile(serverzip_path, 'r')
    for file in f.namelist():
        f.extract(file, serverzip_target_path)
    f.close()   #一定要关闭,不然zipfile一直处于读取压缩包状态
    解压:
    def zip_ya(startdir,file_news): startdir
    = "d:\1" #要压缩的文件夹路径 file_news = startdir +'.zip' # 压缩后文件夹的名字 z = zipfile.ZipFile(file_news,'w',zipfile.ZIP_DEFLATED) #参数一:文件夹名 for dirpath, dirnames, filenames in os.walk(startdir): fpath = dirpath.replace(startdir,'') #这一句很重要,不replace的话,就从根目录开始复制 fpath = fpath and fpath + os.sep or ''#这句话理解我也点郁闷,实现当前文件夹以及包含的所有文件的压缩 for filename in filenames: z.write(os.path.join(dirpath, filename),fpath+filename) print ('压缩成功') z.close() if __name__=="__main__": startdir = "d:\1" #要压缩的文件夹路径 file_news = startdir +'.zip' # 压缩后文件夹的名字 zip_ya(startdir,file_news)
  • 相关阅读:
    微服务全链路剖析
    记一次被挖矿经历
    centos权限总结
    Beizer。。。。。
    遇到的几个算法
    程序截图
    CFileViewer(文件浏览器)
    框架设计
    git常用代码
    右值引用
  • 原文地址:https://www.cnblogs.com/tang-s/p/9682615.html
Copyright © 2011-2022 走看看