简体   繁体   English

使用Python 2.7.5将文件夹中的所有压缩文件解压缩到同一文件夹

[英]Unzip all zipped files in a folder to that same folder using Python 2.7.5

I would like to write a simple script to iterate through all the files in a folder and unzip those that are zipped (.zip) to that same folder. 我想编写一个简单的脚本来遍历文件夹中的所有文件,然后将压缩(.zip)的文件解压缩到同一文件夹。 For this project, I have a folder with nearly 100 zipped .las files and I'm hoping for an easy way to batch unzip them. 对于这个项目,我有一个包含将近100个压缩的.las文件的文件夹,我希望找到一种简单的方法来批量解压缩它们。 I tried with following script 我尝试了以下脚本

import os, zipfile

folder = 'D:/GISData/LiDAR/SomeFolder'
extension = ".zip"

for item in os.listdir(folder):
    if item.endswith(extension):
        zipfile.ZipFile.extract(item)

However, when I run the script, I get the following error: 但是,当我运行脚本时,出现以下错误:

Traceback (most recent call last):
  File "D:/GISData/Tools/MO_Tools/BatchUnzip.py", line 10, in <module>
    extract = zipfile.ZipFile.extract(item)
TypeError: unbound method extract() must be called with ZipFile instance as first argument (got str instance instead)

I am using the python 2.7.5 interpreter. 我正在使用python 2.7.5解释器。 I looked at the documentation for the zipfile module ( https://docs.python.org/2/library/zipfile.html#module-zipfile ) and I would like to understand what I'm doing incorrectly. 我查看了zipfile模块的文档( https://docs.python.org/2/library/zipfile.html#module-zipfile ),我想了解我做错了什么。

I guess in my mind, the process would go something like this: 我想我的过程将是这样的:

  1. Get folder name 获取文件夹名称
  2. Loop through folder and find zip files 循环浏览文件夹并找到zip文件
  3. Extract zip files to folder 将zip文件解压缩到文件夹

Thanks Marcus, however, when implementing the suggestion, I get another error: 感谢Marcus,但是,在实施建议时,出现了另一个错误:

Traceback (most recent call last):
  File "D:/GISData/Tools/MO_Tools/BatchUnzip.py", line 12, in <module>
    zipfile.ZipFile(item).extract()
  File "C:\Python27\ArcGIS10.2\lib\zipfile.py", line 752, in __init__
    self.fp = open(file, modeDict[mode])
IOError: [Errno 2] No such file or directory: 'JeffCity_0752.las.zip'

When I use print statements, I can see that the files are in there. 当我使用打印语句时,我可以看到文件在其中。 For example: 例如:

for item in os.listdir(folder):
    if item.endswith(extension):
        print os.path.abspath(item)
        filename = os.path.basename(item)
        print filename

yields: 产量:

D:\GISData\Tools\MO_Tools\JeffCity_0752.las.zip
JeffCity_0752.las.zip
D:\GISData\Tools\MO_Tools\JeffCity_0753.las.zip
JeffCity_0753.las.zip

As I understand the documentation, 据我了解的文档,

zipfile.ZipFile(file[, mode[, compression[, allowZip64]]])

Open a ZIP file, where file can be either a path to a file (a string) or a file-like object 打开一个ZIP文件,该文件可以是文件的路径(字符串)或类似文件的对象

It appears to me like everything is present and accounted for. 在我看来,一切都存在并得到解释。 I just don't understand what I'm doing wrong. 我只是不明白我在做什么错。

Any suggestions? 有什么建议么?

Thank You 谢谢

Below is the code that worked for me: 以下是对我有用的代码:

import os, zipfile

dir_name = 'C:\\SomeDirectory'
extension = ".zip"

os.chdir(dir_name) # change directory from working dir to dir with files

for item in os.listdir(dir_name): # loop through items in dir
    if item.endswith(extension): # check for ".zip" extension
        file_name = os.path.abspath(item) # get full path of files
        zip_ref = zipfile.ZipFile(file_name) # create zipfile object
        zip_ref.extractall(dir_name) # extract file to dir
        zip_ref.close() # close file
        os.remove(file_name) # delete zipped file

Looking back at the code I had amended, the directory was getting confused with the directory of the script. 回顾我修改过的代码,该目录与脚本目录混淆了。

The following also works while not ruining the working directory. 在不破坏工作目录的情况下,以下内容也适用。 First remove the line 首先删除线

os.chdir(dir_name) # change directory from working dir to dir with files

Then assign file_name as 然后将file_name分配为

file_name = dir_name + "/" + item

You need to construct a ZipFile object with the filename, and then extract it: 您需要ZipFile文件名构造一个ZipFile对象, 然后将其提取:

    zipfile.ZipFile.extract(item)

is wrong. 是错的。

    zipfile.ZipFile(item).extractall()

will extract all files from the zip file with the name contained in item . 将从zip文件中提取所有文件,其中名称为item

I think you should more closely read the documentation to zipfile :) but you're on the right track! 我认为您应该更仔细地阅读zipfile的文档:),但您的方向正确!

The accepted answer works great! 接受的答案效果很好!

Just to extend the idea to unzip all the files with .zip extension within all the sub-directories inside a directory the following code seems to work well: 只是为了扩展想法以解压缩目录内所有子目录内所有扩展名为.zip的文件,以下代码似乎很好用:

import os
import zipfile

for path, dir_list, file_list in os.walk(dir_path):
    for file_name in file_list:
        if file_name.endswith(".zip"):
            abs_file_path = os.path.join(path, file_name)

            # The following three lines of code are only useful if 
            # a. the zip file is to unzipped in it's parent folder and 
            # b. inside the folder of the same name as the file

            parent_path = os.path.split(abs_file_path)[0]
            output_folder_name = os.path.splitext(abs_file_path)[0]
            output_path = os.path.join(parent_path, output_folder_name)

            zip_obj = zipfile.ZipFile(abs_file_path, 'r')
            zip_obj.extractall(output_path)
            zip_obj.close()

I think this is shorter and worked fine for me. 我认为这比较短,对我来说效果很好。 First import the modules required: 首先导入所需的模块:

import zipfile, os

Then, I define the working directory: 然后,我定义工作目录:

working_directory = 'my_directory'
os.chdir(working_directory)

After that you can use a combination of the os and zipfile to get where you want: 之后,您可以结合使用oszipfile到达所需位置:

for file in os.listdir(working_directory):   # get the list of files
    if zipfile.is_zipfile(file): # if it is a zipfile, extract it
        with zipfile.ZipFile(file) as item: # treat the file as a zip
           item.extractall()  # extract it in the working directory

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

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