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

tpd*_*nce 9 python unzip zipfile python-2.7 arcpy

我想编写一个简单的脚本来遍历文件夹中的所有文件,并将压缩文件(.zip)解压缩到同一个文件夹.对于这个项目,我有一个包含近100个压缩.las文件的文件夹,我希望能够轻松地批量解压缩它们.我尝试使用以下脚本

import os, zipfile

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

for item in os.listdir(folder):
    if item.endswith(extension):
        zipfile.ZipFile.extract(item)
Run Code Online (Sandbox Code Playgroud)

但是,当我运行脚本时,我收到以下错误:

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)
Run Code Online (Sandbox Code Playgroud)

我正在使用python 2.7.5解释器.我查看了zipfile模块的文档(https://docs.python.org/2/library/zipfile.html#module-zipfile),我想了解我做错了什么.

我想在我看来,这个过程会是这样的:

  1. 获取文件夹名称
  2. 循环文件夹并查找zip文件
  3. 将zip文件解压缩到文件夹

但是,感谢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'
Run Code Online (Sandbox Code Playgroud)

当我使用print语句时,我可以看到文件在那里.例如:

for item in os.listdir(folder):
    if item.endswith(extension):
        print os.path.abspath(item)
        filename = os.path.basename(item)
        print filename
Run Code Online (Sandbox Code Playgroud)

收益率:

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
Run Code Online (Sandbox Code Playgroud)

据我了解文档,

zipfile.ZipFile(file[, mode[, compression[, allowZip64]]])
Run Code Online (Sandbox Code Playgroud)

打开ZIP文件,其中file可以是文件(字符串)的路径,也可以是类文件对象

在我看来,一切都存在并被解释.我只是不明白我做错了什么.

有什么建议?

谢谢

tpd*_*nce 24

以下代码对我有用:

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
Run Code Online (Sandbox Code Playgroud)

回顾我修改过的代码,目录与脚本目录混淆了.

以下也可以在不破坏工作目录的情况下工作.首先删除该行

os.chdir(dir_name) # change directory from working dir to dir with files
Run Code Online (Sandbox Code Playgroud)

然后将file_name指定为

file_name = dir_name + "/" + item
Run Code Online (Sandbox Code Playgroud)


小智 14

我认为这更短,对我来说效果很好。首先导入所需的模块:

import zipfile, os
Run Code Online (Sandbox Code Playgroud)

然后,我定义工作目录:

working_directory = 'my_directory'
os.chdir(working_directory)
Run Code Online (Sandbox Code Playgroud)

之后,你可以使用的一个组合os,并zipfile以得到你想要的:

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
Run Code Online (Sandbox Code Playgroud)

  • 这个解决方案对我有用。它也比公认的答案更Pythonic。 (6认同)
  • 简短而简单的答案是最好的! (2认同)

0Ni*_*las 6

接受的答案效果很好!

只是为了扩展这个想法以解压缩目录内所有子目录中所有带有 .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()
Run Code Online (Sandbox Code Playgroud)