python - 使用shutil.make_archive压缩文件时出现意外文件

标签 python python-3.x zip shutil

我正在尝试使用shutil.make_archive来压缩文件。

当我使用时:

import shutil
shutil.make_archive('zip_file', 'zip', 'C:\\Users\\test')

我在zip_file中得到了一个zip_file,并且zip_file中的zip_file似乎是一个损坏的文件。有谁知道怎么处理吗?

但是,当我使用时:

make_archive(
  'zipfile_name', 
  'zip',           # the archive format - or tar, bztar, gztar 
  root_dir=file_dir,   # root for archive - current working dir if None
  base_dir=file_dir)   # start archiving from here - cwd if None too

除了文件夹目录错误之外,一切都正确。 例如,如果 file_dir = 'C:\Users\aaa\Desktop\test\bbb' zip 文件的结构为:

 Users  
         aaa
              Desktop
                     test
                         bbb

我希望它只能是 bbb/。

有什么想法吗?我非常感谢您的帮助。

最佳答案

我认为解决方案是在不同的目录中创建 zip 文件。从那里开始,类似

shutil.make_archive('zip_file', 'zip', 'C:\\Users\\test', "bbb")

应该为您提供 bbb 目录的存档。

关于python - 使用shutil.make_archive压缩文件时出现意外文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50156657/

相关文章:

python - 如何在 Python 中检索 pip 要求(卡住)?

python - uwsgi nginx 连接到 unix 套接字被拒绝

Python 3 错误 : ValueError: invalid literal for int() with base 10: ''

python - 如何在两个函数之间共享公共(public)变量

java - 使用java移动文件有哪些方法?

java - Jython - 在 Java 项目中使用 Pygments

python - 大型对象: How to achieve better parallel scaling in python?列表上多处理Pool.map()的缩放比例不佳

python - 使用 matplotlib 创建一个饼图

java - 从 FileChannel (Java NIO) 读取 GZIP 文件

php - 创建一个 zip 文件