Tôi đã thấy các biến thể của câu hỏi này, nhưng không phải trong bối cảnh chính xác này. Những gì tôi có là một tập tin gọi là 100 test.zip chứa 100 .jpg hình ảnh. Tôi muốn mở tệp này trong bộ nhớ và xử lý từng tệp thực hiện các hoạt động PIL. Phần còn lại của mã đã được viết, tôi chỉ muốn tập trung vào từ tệp zip đến hình ảnh PIL đầu tiên. Đây là những gì mã trông giống như bây giờ từ các đề xuất tôi đã thu thập được từ việc đọc các câu hỏi khác, nhưng nó không hoạt động. Các bạn có thể xem xét và giúp đỡ không?
import zipfile
from StringIO import StringIO
from PIL import Image
imgzip = open['100-Test.zip', 'rb']
z = zipfile.ZipFile[imgzip]
data = z.read[z.namelist[][0]]
dataEnc = StringIO[data]
img = Image.open[dataEnc]
print img
Nhưng tôi đang gặp lỗi này khi tôi chạy nó:
IOError: cannot identify image file
Giải pháp thay thế: Tôi đã thấy các nguồn khác nói rằng sử dụng điều này thay thế:
image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
Nhưng vấn đề là tôi không mở một tệp, nó đã có trong bộ nhớ bên trong biến dữ liệu. Tôi cũng đã thử sử dụng
IOError: cannot identify image file
5 nhưng đã gặp lỗi này:TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
Bài viết này giải thích cách người ta có thể thực hiện các hoạt động khác nhau trên tệp zip bằng chương trình Python đơn giản.
Tệp zip là gì?
ZIP là một định dạng tệp lưu trữ hỗ trợ nén dữ liệu không mất. Bằng cách nén không mất, chúng tôi có nghĩa là thuật toán nén cho phép dữ liệu gốc được xây dựng lại hoàn hảo từ dữ liệu được nén. Vì vậy, một tệp zip là một tệp duy nhất chứa một hoặc nhiều tệp nén, cung cấp một cách lý tưởng để tạo các tệp lớn nhỏ hơn và giữ các tệp liên quan lại với nhau.
Tại sao chúng ta cần các tệp zip?
- Để giảm yêu cầu lưu trữ.
- Để cải thiện tốc độ truyền qua các kết nối tiêu chuẩn.
Để làm việc trên các tệp zip bằng Python, chúng tôi sẽ sử dụng mô -đun Python sẵn có gọi là ZipFile.
1. Trích xuất tệp zip
IOError: cannot identify image file
6 IOError: cannot identify image file
7 IOError: cannot identify image file
8 IOError: cannot identify image file
9image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
0image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
1 image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
2image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
3image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
4image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
5image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
6image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
7image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
8image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
6TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
0image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
8TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
2TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
3TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
4TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
5image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
8image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
6TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
8image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
8TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
2TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
3from zipfile import ZipFile2
TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
5Chương trình trên trích xuất một tệp zip có tên là My_Python_files.zip, trong cùng thư mục như tập lệnh Python này. Đầu ra của chương trình trên có thể trông như thế này:
The output of above program may look like this:
Hãy để chúng tôi cố gắng hiểu mã trên theo từng mảnh:
from zipfile import ZipFile
Zipfile là một lớp mô -đun zipfile để đọc và viết các tệp zip. Ở đây chúng tôi chỉ nhập lớp zipfile từ mô -đun zipfile.
with ZipFile[file_name, 'r'] as zip:
Ở đây, một đối tượng zipfile được tạo bằng cách gọi Trình xây dựng ZipFile chấp nhận tên tệp và tham số chế độ.READ mode and name it as zip.
zip.printdir[]
Phương thức printDir [] in một bảng nội dung cho kho lưu trữ. method prints a table of contents for the archive.
zip.extractall[]
Phương thức extractall [] sẽ trích xuất tất cả các nội dung của tệp zip vào thư mục làm việc hiện tại. Bạn cũng có thể gọi phương thức trích xuất [] để trích xuất bất kỳ tệp nào bằng cách chỉ định đường dẫn của nó trong tệp zip. Ví dụ: method will extract all the contents of the zip file to the current working directory. You can also call extract[] method to extract any file by specifying its path in the zip file.
For example:zip.extract['python_files/python_wiki.txt']
Điều này sẽ chỉ trích xuất các tệp được chỉ định.
Nếu bạn muốn đọc một số tệp cụ thể, bạn có thể đi như thế này:
data = zip.read[name_of_file_to_read]
2. Viết vào tệp zip
Xem xét một thư mục [thư mục] với định dạng như vậy:
Ở đây, chúng ta sẽ cần phải thu thập dữ liệu toàn bộ thư mục và các hướng phụ của nó để có được danh sách tất cả các đường dẫn tệp trước khi ghi chúng vào tệp zip.
The following program does this by crawling the directory to be zipped:
IOError: cannot identify image file
6 IOError: cannot identify image file
7 IOError: cannot identify image file
8 IOError: cannot identify image file
9image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
0image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
1 image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
2Chương trình trên trích xuất một tệp zip có tên là My_Python_files.zip, trong cùng thư mục như tập lệnh Python này. Đầu ra của chương trình trên có thể trông như thế này:
Hãy để chúng tôi cố gắng hiểu mã trên theo từng mảnh:
Zipfile là một lớp mô -đun zipfile để đọc và viết các tệp zip. Ở đây chúng tôi chỉ nhập lớp zipfile từ mô -đun zipfile.
Ở đây, một đối tượng zipfile được tạo bằng cách gọi Trình xây dựng ZipFile chấp nhận tên tệp và tham số chế độ.
Phương thức printDir [] in một bảng nội dung cho kho lưu trữ.
zip.printdir[]6
zip.extractall[]1
Phương thức extractall [] sẽ trích xuất tất cả các nội dung của tệp zip vào thư mục làm việc hiện tại. Bạn cũng có thể gọi phương thức trích xuất [] để trích xuất bất kỳ tệp nào bằng cách chỉ định đường dẫn của nó trong tệp zip. Ví dụ:
Điều này sẽ chỉ trích xuất các tệp được chỉ định.
Nếu bạn muốn đọc một số tệp cụ thể, bạn có thể đi như thế này:
image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
8with ZipFile[file_name, 'r'] as zip:3
image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
1 zip.extract['python_files/python_wiki.txt']4
image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
8TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
2TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
3zip.extract['python_files/python_wiki.txt']8
TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
5image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
8with ZipFile[file_name, 'r'] as zip:7
image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
0with ZipFile[file_name, 'r'] as zip:9
data = zip.read[name_of_file_to_read]4
zip.printdir[]1
TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
2data = zip.read[name_of_file_to_read]7
Các
zip.printdir[]1
with ZipFile[file_name, 'r'] as zip:7
IOError: cannot identify image file
08 with ZipFile[file_name, 'r'] as zip:9
data = zip.read[name_of_file_to_read]4
zip.printdir[]6
image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
6 IOError: cannot identify image file
13 IOError: cannot identify image file
08TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
5image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
8TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
2TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
3 IOError: cannot identify image file
19 IOError: cannot identify image file
20 IOError: cannot identify image file
21 IOError: cannot identify image file
222.image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
8 IOError: cannot identify image file
28Đầu ra của chương trình trên trông như thế này:
Hãy để chúng tôi cố gắng hiểu mã ở trên bằng cách chia thành các mảnh:
0IOError: cannot identify image file
Trước hết, để có được tất cả các đường dẫn tệp trong thư mục của chúng tôi, chúng tôi đã tạo chức năng này sử dụng phương thức Os.Walk [] & NBSP; Trong mỗi lần lặp, tất cả các tệp có trong thư mục đó được thêm vào một danh sách có tên File_path. Trong phần cuối, chúng tôi trả về tất cả các đường dẫn tệp.os.walk[] method. In each iteration, all files present in that directory are appended to a list called file_paths.
In the end, we return all the file paths.
1IOError: cannot identify image file
Ở đây chúng tôi chuyển thư mục để được nén đến hàm GET_ALL_FILE_PATHS [] và có được danh sách chứa tất cả các đường dẫn tệp.get_all_file_paths[] function and obtain a list containing all file paths.
2IOError: cannot identify image file
Ở đây, chúng tôi tạo một đối tượng zipfile trong chế độ ghi lần này.
3IOError: cannot identify image file
Ở đây, chúng tôi viết tất cả các tệp vào tệp zip từng một bằng phương thức ghi. write method.
3. Nhận tất cả thông tin về tệp zip
IOError: cannot identify image file
6 IOError: cannot identify image file
7 IOError: cannot identify image file
8 IOError: cannot identify image file
9 IOError: cannot identify image file
8 IOError: cannot identify image file
34image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
0image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
1 IOError: cannot identify image file
37image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
3image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
4image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
5image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
6image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
7image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
8with ZipFile[file_name, 'r'] as zip:7
IOError: cannot identify image file
45with ZipFile[file_name, 'r'] as zip:9
image_file = StringIO[open["test.jpg",'rb'].read[]]
im = Image.open[image_file]
6 IOError: cannot identify image file
48zip.printdir[]6
TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
2 IOError: cannot identify image file
51
zip.printdir[]6
TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
2TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
3 IOError: cannot identify image file
55 IOError: cannot identify image file
56 IOError: cannot identify image file
57 IOError: cannot identify image file
58 IOError: cannot identify image file
59 IOError: cannot identify image file
60zip.printdir[]6
TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
2TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
3 IOError: cannot identify image file
64 IOError: cannot identify image file
56 IOError: cannot identify image file
57__zip.printdir[]6
TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
2TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
3 IOError: cannot identify image file
74 IOError: cannot identify image file
56 IOError: cannot identify image file
57 IOError: cannot identify image file
77zip.printdir[]6
TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
2TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
3 IOError: cannot identify image file
81 IOError: cannot identify image file
56 IOError: cannot identify image file
57__zip.printdir[]6
TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
2TypeError: unbound method read[] must be called with StringIO instance as
first argument [got str instance instead]
3 IOError: cannot identify image file
91 IOError: cannot identify image file
56 IOError: cannot identify image file
57__Đầu ra của chương trình trên có thể trông như thế này:
IOError: cannot identify image file
4Ở đây, phương thức InfoList [] tạo một thể hiện của lớp zipinfo chứa tất cả thông tin về tệp zip. Chúng ta có thể truy cập tất cả thông tin như ngày sửa đổi cuối cùng của tệp, tên tệp, hệ thống trên đó các tệp được tạo, phiên bản zip, kích thước của tệp ở dạng nén và không nén, v.v.infolist[] method creates
an instance of ZipInfo class which contains all the information about the zip file.
We can access all information like last modification date of files, file names, system on which files were created, Zip version, size of files in compressed and uncompressed form, etc.
Bài viết này & nbsp; được đóng góp bởi Nikhil Kumar. Nếu bạn thích GeekSforGeeks và muốn đóng góp, bạn cũng có thể viết một bài viết bằng Write.GeekSforGeek.org hoặc gửi bài viết của bạn. Xem bài viết của bạn xuất hiện trên trang chính của GeekSforGeek và giúp các chuyên viên máy tính khác.Nikhil Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
Vui lòng viết nhận xét nếu bạn tìm thấy bất cứ điều gì không chính xác, hoặc bạn muốn chia sẻ thêm thông tin về chủ đề được thảo luận ở trên.