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)
  • 相关阅读:
    UVA 254 Towers of Hanoi
    UVA 701 The Archeologists' Dilemma
    UVA 185 Roman Numerals
    UVA 10994 Simple Addition
    UVA 10570 Meeting with Aliens
    UVA 306 Cipher
    UVA 10160 Servicing Stations
    UVA 317 Hexagon
    UVA 10123 No Tipping
    UVA 696 How Many Knights
  • 原文地址:https://www.cnblogs.com/tang-s/p/9682615.html
Copyright © 2011-2022 走看看