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)
  • 相关阅读:
    HDU 3949 XOR
    [JXOI2018]游戏
    树状数组 Binary Indexed Tree/Fenwick Tree
    Java 多线程编程
    概率算法
    最长回文子串 Manacher算法
    动态规划-最长上升子序列 LIS
    流水作业调度
    多机调度问题
    A*搜索算法
  • 原文地址:https://www.cnblogs.com/tang-s/p/9682615.html
Copyright © 2011-2022 走看看