Tôi muốn viết một tập lệnh đơn giản để lặp qua tất cả các tệp trong một thư mục và giải nén các tập tin được nén [.zip] vào cùng một thư mục. Đối với dự án này, tôi có một thư mục với gần 100 tệp .las và tôi hy vọng cho một cách dễ dàng để giải nén chúng. Tôi đã thử với tập lệnh sau
import os, zipfile
folder = 'D:/GISData/LiDAR/SomeFolder'
extension = ".zip"
for item in os.listdir[folder]:
if item.endswith[extension]:
zipfile.ZipFile.extract[item]
Tuy nhiên, khi tôi chạy tập lệnh, tôi gặp lỗi sau:
Traceback [most recent call last]:
File "D:/GISData/Tools/MO_Tools/BatchUnzip.py", line 10, in
extract = zipfile.ZipFile.extract[item]
TypeError: unbound method extract[] must be called with ZipFile instance as first argument [got str instance instead]
Tôi đang sử dụng trình thông dịch Python 2.7.5. Tôi đã xem tài liệu cho mô-đun zipfile [//docs.python.org/2/l Library/zipfile.html#module-zipfile] và tôi muốn hiểu những gì tôi đang làm không chính xác.
Tôi đoán trong tâm trí của tôi, quá trình sẽ diễn ra như thế này:
- Nhận tên thư mục
- Vòng lặp qua thư mục và tìm các tệp zip
- Trích xuất các tệp zip vào thư mục
Tuy nhiên, cảm ơn Marcus khi thực hiện đề xuất, tôi gặp một lỗi khác:
Traceback [most recent call last]:
File "D:/GISData/Tools/MO_Tools/BatchUnzip.py", line 12, in
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'
Khi tôi sử dụng các câu lệnh in, tôi có thể thấy rằng các tệp ở đó. Ví dụ:
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
Theo tôi hiểu tài liệu,
zipfile.ZipFile[file[, mode[, compression[, allowZip64]]]]
Mở tệp zip, trong đó tệp có thể là đường dẫn đến tệp [chuỗi] hoặc đối tượng giống như tệp
Nó xuất hiện với tôi như mọi thứ đều có mặt và chiếm. Tôi chỉ không hiểu những gì tôi đang làm sai.
Bất kỳ đề xuất?
Cảm ơn
Trong bài viết này, chúng tôi sẽ thảo luận về các cách khác nhau để giải nén hoặc trích xuất đơn, nhiều hoặc tất cả các tệp từ kho lưu trữ zip đến thư mục hiện tại hoặc khác nhau.
Trong mô -đun Zipfile của Python, lớp Zipfile cung cấp chức năng thành viên để trích xuất tất cả các nội dung từ kho lưu trữ zip,
ZipFile.extractall[path=None, members=None, pwd=None]
Nó chấp nhận các đối số sau:
- Thành viên: Tên đầy đủ của tập tin sẽ được trích xuất. Nó nên một từ danh sách các tên tệp được lưu trữ được trả về bởi zipfile.namelist [] : location where zip file need to be extracted, if not provided it will extract the contents in current directory.
- Đường dẫn: Vị trí cần trích xuất tệp zip, nếu không được cung cấp, nó sẽ trích xuất tệp trong thư mục hiện tại. : list of files to be extracted. It will extract all the files in zip if this argument is not provided.
- PWD: Nếu tệp zip được mã hóa thì hãy truyền mật khẩu trong mặc định đối số này là không có. : If zip file is encrypted then pass password in this argument default is None.
Hãy để sử dụng điều này để chỉ trích xuất các tệp CSV từ tệp zip, tức là.
from zipfile import ZipFile
Nó sẽ chỉ trích xuất các tệp CSV từ kho lưu trữ zip đã cho.
Ví dụ hoàn chỉnh như sau,
Traceback [most recent call last]:
File "D:/GISData/Tools/MO_Tools/BatchUnzip.py", line 10, in
extract = zipfile.ZipFile.extract[item]
TypeError: unbound method extract[] must be called with ZipFile instance as first argument [got str instance instead]
2 & nbsp;To unzip it first create a ZipFile object by opening the zip file in read mode and then call extractall[] on that object i.e.
# Create a ZipFile Object and load sample.zip in it with ZipFile['sampleDir.zip', 'r'] as zipObj: # Extract all the contents of zip file in current directory zipObj.extractall[]
Làm cách nào để giải nén tất cả các tệp trong một thư mục trong Python?
Quảng cáo
Trích xuất tất cả các tệp từ tệp zip sang thư mục khác nhau
Để trích xuất tất cả các tệp từ tệp ZIP sang một thư mục khác, chúng ta có thể chuyển vị trí đích dưới dạng đối số trong Extractall []. Đường dẫn có thể là tương đối hoặc tuyệt đối.
# Create a ZipFile Object and load sample.zip in it with ZipFile['sampleDir.zip', 'r'] as zipObj: # Extract all the contents of zip file in different directory zipObj.extractall['temp']
Nó sẽ trích xuất tất cả các tệp trong ‘sample.zip, in & nbsp; Thư mục tạm thời.temp folder.
Trích xuất một vài tệp từ một tệp zip lớn dựa trên điều kiện
Giả sử chúng ta có một tệp zip rất lớn và chúng ta cần một vài tệp từ hàng ngàn tệp trong kho lưu trữ. Giải nén tất cả các tập tin từ zip lớn có thể mất vài phút. Nhưng nếu chỉ quan tâm đến một vài tệp được lưu trữ, thì & nbsp; thay vì giải nén toàn bộ tệp, chúng ta cũng có thể trích xuất một tệp duy nhất từ tệp zip.
Trong mô -đun Zipfile của Python, lớp Zipfile cung cấp chức năng thành viên để trích xuất một đơn từ tệp zip,
Traceback [most recent call last]:
File "D:/GISData/Tools/MO_Tools/BatchUnzip.py", line 10, in
extract = zipfile.ZipFile.extract[item]
TypeError: unbound method extract[] must be called with ZipFile instance as first argument [got str instance instead]
0Nó chấp nhận các đối số sau:
- Thành viên: Tên đầy đủ của tập tin sẽ được trích xuất. Nó nên một từ danh sách các tên tệp được lưu trữ được trả về bởi zipfile.namelist [] : Full name of file to be extracted. It should one from the list of archived files names returned by ZipFile.namelist[]
- Đường dẫn: Vị trí cần trích xuất tệp zip, nếu không được cung cấp, nó sẽ trích xuất tệp trong thư mục hiện tại. location where zip file need to be extracted, if not provided it will extract the file in current directory.
- PWD: Nếu tệp zip được mã hóa thì hãy truyền mật khẩu trong mặc định đối số này là không có. If zip file is encrypted then pass password in this argument default is None.
Hãy để sử dụng điều này để chỉ trích xuất các tệp CSV từ tệp zip, tức là.
Traceback [most recent call last]:
File "D:/GISData/Tools/MO_Tools/BatchUnzip.py", line 10, in
extract = zipfile.ZipFile.extract[item]
TypeError: unbound method extract[] must be called with ZipFile instance as first argument [got str instance instead]
1Nó sẽ chỉ trích xuất các tệp CSV từ kho lưu trữ zip đã cho.
Ví dụ hoàn chỉnh như sau,
Traceback [most recent call last]:
File "D:/GISData/Tools/MO_Tools/BatchUnzip.py", line 10, in
extract = zipfile.ZipFile.extract[item]
TypeError: unbound method extract[] must be called with ZipFile instance as first argument [got str instance instead]
2 & nbsp;