相关疑难解决方法(0)

使用shutil.make_archive()压缩目录,同时保留目录结构

我正在尝试将名为test_dicomszip文件的目录压缩为test_dicoms.zip使用以下代码命名的zip文件:

shutil.make_archive('/home/code/test_dicoms','zip','/home/code/test_dicoms')

问题是,当我解压缩它时,所有文件都/test_dicoms/被提取到/home/code/而不是文件夹,/test_dicoms/并且所有文件都被提取到/home/code/.所以/test_dicoms/有一个文件调用foo.txt,在我压缩和解压缩foo.txt的路径之后是/home/code/foo.txt相反的/home/code/test_dicoms/foo.txt.我该如何解决?此外,我正在使用的一些目录非常大.我是否需要在代码中添加任何内容以使其成为ZIP64,或者是否足够自动地执行此操作?

以下是创建的存档中的内容:

[gwarner@jazz gwarner]$ unzip -l test_dicoms.zip
Archive: test_dicoms.zip
Length    Date       Time  Name
--------- ---------- ----- ----
    93324 09-17-2015 16:05 AAscout_b_000070
    93332 09-17-2015 16:05 AAscout_b_000125
    93332 09-17-2015 16:05 AAscout_b_000248
Run Code Online (Sandbox Code Playgroud)

python directory zip shutil

24
推荐指数
4
解决办法
3万
查看次数

使用给定目录中的目录路径提取所有文件

我有一个tar存档,我有一个目录,我需要在给定的目录中提取.例如:我有一个目录

TarPrefix/X/Y/Z

在tar存档中我想在给定的目标目录中提取它,例如:extract/a/this目录应该包含目录TarPrefix/x/y/z中包含的所有文件和目录.

subdir_and_files = [  tarinfo for tarinfo in tar.getmembers()
                      if tarinfo.name.startswith("subfolder/")
                   ]
Run Code Online (Sandbox Code Playgroud)

获取目录路径"子文件夹/"中所有成员的列表然后我使用tar.extractall(extracted/a,subdir_and_files) 它提取它,但它提取所有成员及其目录路径例如,这导致提取/ a/x/y/z.你能帮我解压一下给定文件夹中的这些文件吗?

python extract tar

7
推荐指数
1
解决办法
1万
查看次数

标签 统计

python ×2

directory ×1

extract ×1

shutil ×1

tar ×1

zip ×1