解压缩每个文件夹的每个子目录的文件

2024-10-02 14:29:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个目录树,其结构如下所示:

-文件夹1
-------子文件夹1
----------------------拉链
-------子文件夹2
----------------------压缩的bzfile
-文件夹2
-------子文件夹1
----------------------压缩的bzfile
-------子文件夹2
----------------------压缩的bzfile

以此类推。。你知道吗

我的目标是解压缩文件夹1、2、3[…]和10的所有子目录的每个zipfile。你知道吗

算法应该是这个,但我卡住了。。你知道吗

import tar
for each folder
    for each subfolder
        tarfile.open(each_file)
        tar.extractall(in_the_subdirectory_of_the_tarfile)
        tar.close

你有什么办法让它工作吗?
谢谢!你知道吗


Tags: theimport目录文件夹算法目标fortar
1条回答
网友
1楼 · 发布于 2024-10-02 14:29:13

试试这个:

import tar
import os

path = "."

for root, dirs, files in os.walk(path):
    for name in files:
        file = os.path.join(root, name)
        tarfile.open(file)
        tar.extractall(in_the_subdirectory_of_the_tarfile)
        tar.close

相关问题 更多 >