繁体   English   中英

zip 文件夹的并取文件夹的名称

[英]zip folder's and take the name of folder

我正在尝试制作此代码,该代码获取目录中每个文件夹的内容并将其与文件夹名称一一添加到 zip

我确实编写了这段代码,但我被阻止了,只是在 zip 中按扩展名添加文件

import zipfile, os

handle = zipfile.ZipFile('ALL-PY.zip', 'w')

for x in os.listdir():
    if x.endswith(directory):
         handle.write(x,compress_type = zipfile.ZIP_DEFLATED)

handle.close()

我会遵循这种方法:

import zipfile, os

handle = zipfile.ZipFile('ALL-PY.zip', 'w')

path = "C:/Users/User_Name/my_directory"

os.chdir(path)

for directory, subs, files in os.walk("."):
    handle.write(directory)
    for this_file in files:
        handle.write(os.path.join(directory, this_file), compress_type = zipfile.ZIP_DEFLATED)

handle.close()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM