关于python:python-文件压缩

102次阅读

共计 1189 个字符,预计需要花费 3 分钟才能阅读完成。

import zipfile,os
def un_zip(file_name):
    """unzip zip file"""
    zip_file = zipfile.ZipFile(file_name)
    if os.path.isdir(os.path.splitext(file_name)[0]):
        pass
    else:
        os.mkdir(os.path.splitext(file_name)[0])
    for names in zip_file.namelist():
        zip_file.extract(names,os.path.splitext(file_name)[0])
    zip_file.close()

def compress(get_files_path, set_files_path,pwd):
    print("正在压缩....")
    f = zipfile.ZipFile(set_files_path, 'w', zipfile.ZIP_DEFLATED)
    if os.path.isfile(get_files_path):
        f.write(get_files_path,os.path.basename(get_files_path))
    else:
      for dirpath, dirnames, filenames in os.walk(get_files_path):
        fpath = dirpath.replace(get_files_path,'')
        fpath = fpath and fpath + os.sep or ''
        for filename in filenames:
            f.write(os.path.join(dirpath,filename), fpath+filename)
    f.close()

    print("压缩实现")

    input("Prease <enter>")


def checkfile(get_files_path):
    path=get_files_path.strip("\"")
    if os.path.exists(path)==False:
        print("门路输出谬误")
        get_files_path = input("从新输出须要解决的文件门路:")# 须要压缩的文件夹
        checkfile(get_files_path)
    else:
        if os.path.splitext(path)[1] == ".zip":
            un_zip(path)
        else:
            # 寄存的压缩文件地址 (留神: 不能与上述压缩文件夹一样)
            set_files_path = path+".zip"
            compress(path, set_files_path,pwd)


if __name__=='__main__':
    pwd = input("使用者明码:")# 须要压缩的文件夹
    get_files_path = input("须要解决的文件门路:")# 须要压缩的文件夹
    checkfile(get_files_path)

正文完
 0