![](/img/trans.png)
[英]Extract all files from a zipped folder inside a directory to other directory without folder using python
[英]Unzip all zipped files in a folder to that same folder using Python 2.7.5
我想編寫一個簡單的腳本來遍歷文件夾中的所有文件,然后將壓縮(.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)
但是,當我運行腳本時,出現以下錯誤:
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)
我正在使用python 2.7.5解釋器。 我查看了zipfile模塊的文檔( https://docs.python.org/2/library/zipfile.html#module-zipfile ),我想了解我做錯了什么。
我想我的過程將是這樣的:
感謝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'
當我使用打印語句時,我可以看到文件在其中。 例如:
for item in os.listdir(folder):
if item.endswith(extension):
print os.path.abspath(item)
filename = os.path.basename(item)
print filename
產量:
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
據我了解的文檔,
zipfile.ZipFile(file[, mode[, compression[, allowZip64]]])
打開一個ZIP文件,該文件可以是文件的路徑(字符串)或類似文件的對象
在我看來,一切都存在並得到解釋。 我只是不明白我在做什么錯。
有什么建議么?
謝謝
以下是對我有用的代碼:
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
回顧我修改過的代碼,該目錄與腳本目錄混淆了。
在不破壞工作目錄的情況下,以下內容也適用。 首先刪除線
os.chdir(dir_name) # change directory from working dir to dir with files
然后將file_name分配為
file_name = dir_name + "/" + item
您需要ZipFile
文件名構造一個ZipFile
對象, 然后將其提取:
zipfile.ZipFile.extract(item)
是錯的。
zipfile.ZipFile(item).extractall()
將從zip文件中提取所有文件,其中名稱為item
。
我認為您應該更仔細地閱讀zipfile
的文檔:),但您的方向正確!
接受的答案效果很好!
只是為了擴展想法以解壓縮目錄內所有子目錄內所有擴展名為.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()
我認為這比較短,對我來說效果很好。 首先導入所需的模塊:
import zipfile, os
然后,我定義工作目錄:
working_directory = 'my_directory'
os.chdir(working_directory)
之后,您可以結合使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.