简体   繁体   English

如何解压缩相同子目录但不同文件夹中的文件

[英]How to unzip files in the same subdirectories but a different folder

In my_directory I have 3 folders (NY, AMS, MAD).my_directory我有 3 个文件夹(NY、AMS、MAD)。 Each folder has 1 or more zipped files.每个文件夹都有 1 个或多个压缩文件。 I also have an other directory named my_counterpart .我还有一个名为my_counterpart目录。 This one is empty.这个是空的。

With below code Im trying to:使用下面的代码我试图:

  1. Collect all the 3 folders and the zipped files they posses.收集所有 3 个文件夹和它们拥有的压缩文件。
  2. Copy the 3 folders to my_counterpart + unzipp the files they posses`.将 3 个文件夹复制到my_counterpart + 解压缩它们拥有的文件。

This is my code:这是我的代码:

pattern = '*.zip'
for root, dirs, files in os.walk(my_directory): 
    for filename in fnmatch.filter(files, pattern): 
        path = os.path.join(root, filename) 
        new = os.path.join(my_counterpart, dirs)
        zipfile.ZipFile(path).extractall(new) 

I know where the fault lies, dirs is not aa string but a list.我知道问题出在哪里, dirs不是字符串而是列表。 However I can't seem to solve it.但是我似乎无法解决它。 Someone here who can guide me?这里有人可以指导我吗?

TypeError: join() argument must be str or bytes, not 'list'

Does the variable my_counterpart countain the path to new folder?变量my_counterpart是否计入新文件夹的路径? If yes then why would you add something else to it like dirs ?如果是,那么您为什么要向其添加其他内容,例如dirs Leaving it out already does what you want it to do.离开它已经可以做你想让它做的事情。 What's left to do is to create the folder structure and extract into that newly created folder structure:剩下要做的是创建文件夹结构并提取到新创建的文件夹结构中:

pattern = '*.zip'
for root, dirs, files in os.walk(my_directory): 
    for filename in fnmatch.filter(files, pattern): 
        path = os.path.join(root, filename)

        # Store the new directory so that it can be recreated
        new_dir = os.path.normpath(os.path.join(os.path.relpath(path, start=my_directory), ".."))

        # Join your target directory with newly created directory
        new = os.path.join(my_counterpart, new_dir)

        # Create those folders, works even with nested folders
        if (not os.path.exists(new)):
            os.makedirs(new)

        zipfile.ZipFile(path).extractall(new) 

暂无
暂无

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

相关问题 使用Python 2.7.5将文件夹中的所有压缩文件解压缩到同一文件夹 - Unzip all zipped files in a folder to that same folder using Python 2.7.5 如何在Python中解压缩文件但保留Zip文件夹 - How to Unzip files in Python but Keep Zip Folder 如何在多个子目录中找到具有相同扩展名的所有文件并使用 python 将它们移动到单独的文件夹中? - How can find all files of the same extension within multiple subdirectories and move them to a seperate folder using python? 如何比较2个不同文件夹中2个同名的不同excel文件? - How to compare 2 different excel files of same name in 2 different folder? 我有不同的zip文件,每个文件包含一个csv文件。 如何解压缩每个文件夹,并将所有csv文件保存在一个文件夹中 - I have different zip files that contain one csv file each. how do I unzip each folder, and save all the csv files in one folder 如何解压 Python 中的文件? - How to unzip files in Python? 如果同一文件夹中存在两个具有相同名称和不同结尾的文件,如何从文件夹中删除一个文件? - How to delete one file from a folder, if two files with the same name and different ending exist in the same folder? 如何将存储在目录及其子目录中的文件移动到不同的目录? - How to move files stored in a directory and its subdirectories to a different directory? 如何在colab中解压缩图像文件夹 - How to unzip image folder in colab 如何解压缩传递变量的文件夹? - How to unzip a folder passing a variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM