Python - load an in-memory ZipFile object as bytes
up vote
1
down vote
favorite
I have a script which creates a closed in-memory ZipFile object that I need to post as a bytestring (using requests); how do I do that? I have tried opening the file, which fails with "TypeError: expected str, bytes or os.PathLike object, not ZipFile"
The script works just fine if I write the ZipFile to a file and then open that file for the post data. However it will probably iterate over a couple million files, and that seems like a lot of temp files, and disk activity.
import io
import zipfile
from PIL import Image
z = io.BytesIO()
zfile = zipfile.ZipFile(z,"a")
zipdict = {}
img_loc = "D:/Images/seasons-3.jpg"
im_original = Image.open(img_loc)
imfmt = im_original.format
im = im_original.copy()
im_original.close()
im_out = io.BytesIO()
im.save(im_out,imfmt)
zfile.writestr("seasons-3.jpg",im_out.getvalue())
im_out.close()
zipdict['seasons-3']=zfile
zfile.close()
running with error:
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
>>> zipdict['seasons-3']
<zipfile.ZipFile [closed]>
>>> pl_data = open(zipdict['seasons-3'])
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
pl_data = open(zipdict['seasons-3'])
TypeError: expected str, bytes or os.PathLike object, not ZipFile
>>>
python python-requests zipfile bytesio
New contributor
|
show 4 more comments
up vote
1
down vote
favorite
I have a script which creates a closed in-memory ZipFile object that I need to post as a bytestring (using requests); how do I do that? I have tried opening the file, which fails with "TypeError: expected str, bytes or os.PathLike object, not ZipFile"
The script works just fine if I write the ZipFile to a file and then open that file for the post data. However it will probably iterate over a couple million files, and that seems like a lot of temp files, and disk activity.
import io
import zipfile
from PIL import Image
z = io.BytesIO()
zfile = zipfile.ZipFile(z,"a")
zipdict = {}
img_loc = "D:/Images/seasons-3.jpg"
im_original = Image.open(img_loc)
imfmt = im_original.format
im = im_original.copy()
im_original.close()
im_out = io.BytesIO()
im.save(im_out,imfmt)
zfile.writestr("seasons-3.jpg",im_out.getvalue())
im_out.close()
zipdict['seasons-3']=zfile
zfile.close()
running with error:
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
>>> zipdict['seasons-3']
<zipfile.ZipFile [closed]>
>>> pl_data = open(zipdict['seasons-3'])
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
pl_data = open(zipdict['seasons-3'])
TypeError: expected str, bytes or os.PathLike object, not ZipFile
>>>
python python-requests zipfile bytesio
New contributor
1
Can you paste your code and error traceback?
– Cheche
yesterday
2
"closed in-memory ZipFile" - you're going to have to explain that some more. Did you wrap a ZipFile around a BytesIO or something?
– user2357112
yesterday
I'm not sure if it helps with an in-memory zip-file (never encountered one in the wild), but you can unzip a single file from an archive: stackoverflow.com/a/46423414/962190
– Arne
yesterday
@user2357112 that's pretty much exactly what I did. I created a ZipFile and used writestr to add a couple of BytesIO to the ZipFile. Then I added the ZipFile as value to a dict, with key as filename, and closed the ZipFile.
– Tim Achee
19 hours ago
@TimAchee: Nope, that still doesn't explain things. What, if anything, did you do to put the ZipFile itself in memory? What arguments did you pass to the ZipFile constructor?
– user2357112
19 hours ago
|
show 4 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a script which creates a closed in-memory ZipFile object that I need to post as a bytestring (using requests); how do I do that? I have tried opening the file, which fails with "TypeError: expected str, bytes or os.PathLike object, not ZipFile"
The script works just fine if I write the ZipFile to a file and then open that file for the post data. However it will probably iterate over a couple million files, and that seems like a lot of temp files, and disk activity.
import io
import zipfile
from PIL import Image
z = io.BytesIO()
zfile = zipfile.ZipFile(z,"a")
zipdict = {}
img_loc = "D:/Images/seasons-3.jpg"
im_original = Image.open(img_loc)
imfmt = im_original.format
im = im_original.copy()
im_original.close()
im_out = io.BytesIO()
im.save(im_out,imfmt)
zfile.writestr("seasons-3.jpg",im_out.getvalue())
im_out.close()
zipdict['seasons-3']=zfile
zfile.close()
running with error:
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
>>> zipdict['seasons-3']
<zipfile.ZipFile [closed]>
>>> pl_data = open(zipdict['seasons-3'])
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
pl_data = open(zipdict['seasons-3'])
TypeError: expected str, bytes or os.PathLike object, not ZipFile
>>>
python python-requests zipfile bytesio
New contributor
I have a script which creates a closed in-memory ZipFile object that I need to post as a bytestring (using requests); how do I do that? I have tried opening the file, which fails with "TypeError: expected str, bytes or os.PathLike object, not ZipFile"
The script works just fine if I write the ZipFile to a file and then open that file for the post data. However it will probably iterate over a couple million files, and that seems like a lot of temp files, and disk activity.
import io
import zipfile
from PIL import Image
z = io.BytesIO()
zfile = zipfile.ZipFile(z,"a")
zipdict = {}
img_loc = "D:/Images/seasons-3.jpg"
im_original = Image.open(img_loc)
imfmt = im_original.format
im = im_original.copy()
im_original.close()
im_out = io.BytesIO()
im.save(im_out,imfmt)
zfile.writestr("seasons-3.jpg",im_out.getvalue())
im_out.close()
zipdict['seasons-3']=zfile
zfile.close()
running with error:
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
>>> zipdict['seasons-3']
<zipfile.ZipFile [closed]>
>>> pl_data = open(zipdict['seasons-3'])
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
pl_data = open(zipdict['seasons-3'])
TypeError: expected str, bytes or os.PathLike object, not ZipFile
>>>
python python-requests zipfile bytesio
python python-requests zipfile bytesio
New contributor
New contributor
edited 18 hours ago
New contributor
asked yesterday
Tim Achee
122
122
New contributor
New contributor
1
Can you paste your code and error traceback?
– Cheche
yesterday
2
"closed in-memory ZipFile" - you're going to have to explain that some more. Did you wrap a ZipFile around a BytesIO or something?
– user2357112
yesterday
I'm not sure if it helps with an in-memory zip-file (never encountered one in the wild), but you can unzip a single file from an archive: stackoverflow.com/a/46423414/962190
– Arne
yesterday
@user2357112 that's pretty much exactly what I did. I created a ZipFile and used writestr to add a couple of BytesIO to the ZipFile. Then I added the ZipFile as value to a dict, with key as filename, and closed the ZipFile.
– Tim Achee
19 hours ago
@TimAchee: Nope, that still doesn't explain things. What, if anything, did you do to put the ZipFile itself in memory? What arguments did you pass to the ZipFile constructor?
– user2357112
19 hours ago
|
show 4 more comments
1
Can you paste your code and error traceback?
– Cheche
yesterday
2
"closed in-memory ZipFile" - you're going to have to explain that some more. Did you wrap a ZipFile around a BytesIO or something?
– user2357112
yesterday
I'm not sure if it helps with an in-memory zip-file (never encountered one in the wild), but you can unzip a single file from an archive: stackoverflow.com/a/46423414/962190
– Arne
yesterday
@user2357112 that's pretty much exactly what I did. I created a ZipFile and used writestr to add a couple of BytesIO to the ZipFile. Then I added the ZipFile as value to a dict, with key as filename, and closed the ZipFile.
– Tim Achee
19 hours ago
@TimAchee: Nope, that still doesn't explain things. What, if anything, did you do to put the ZipFile itself in memory? What arguments did you pass to the ZipFile constructor?
– user2357112
19 hours ago
1
1
Can you paste your code and error traceback?
– Cheche
yesterday
Can you paste your code and error traceback?
– Cheche
yesterday
2
2
"closed in-memory ZipFile" - you're going to have to explain that some more. Did you wrap a ZipFile around a BytesIO or something?
– user2357112
yesterday
"closed in-memory ZipFile" - you're going to have to explain that some more. Did you wrap a ZipFile around a BytesIO or something?
– user2357112
yesterday
I'm not sure if it helps with an in-memory zip-file (never encountered one in the wild), but you can unzip a single file from an archive: stackoverflow.com/a/46423414/962190
– Arne
yesterday
I'm not sure if it helps with an in-memory zip-file (never encountered one in the wild), but you can unzip a single file from an archive: stackoverflow.com/a/46423414/962190
– Arne
yesterday
@user2357112 that's pretty much exactly what I did. I created a ZipFile and used writestr to add a couple of BytesIO to the ZipFile. Then I added the ZipFile as value to a dict, with key as filename, and closed the ZipFile.
– Tim Achee
19 hours ago
@user2357112 that's pretty much exactly what I did. I created a ZipFile and used writestr to add a couple of BytesIO to the ZipFile. Then I added the ZipFile as value to a dict, with key as filename, and closed the ZipFile.
– Tim Achee
19 hours ago
@TimAchee: Nope, that still doesn't explain things. What, if anything, did you do to put the ZipFile itself in memory? What arguments did you pass to the ZipFile constructor?
– user2357112
19 hours ago
@TimAchee: Nope, that still doesn't explain things. What, if anything, did you do to put the ZipFile itself in memory? What arguments did you pass to the ZipFile constructor?
– user2357112
19 hours ago
|
show 4 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
zfile
is closed. It's useless to you. The thing you need to use now is z
, the file-like object that was managing the underlying binary storage for the ZipFile.
You can use z.getvalue()
to get a bytestring representing the contents of z
, just like you did with im_out
, or you can seek back to the beginning with z.seek(0)
and use it with the parts of requests
that take file-like objects.
thank you user2357112, but I need to post the data as a bytestring of a zipped file so I'm not sure this solution would work out.
– Tim Achee
17 hours ago
@TimAchee: What makes you think this wouldn't work? It sounds like you're misunderstanding the role of the ZipFile object.
– user2357112
7 hours ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
zfile
is closed. It's useless to you. The thing you need to use now is z
, the file-like object that was managing the underlying binary storage for the ZipFile.
You can use z.getvalue()
to get a bytestring representing the contents of z
, just like you did with im_out
, or you can seek back to the beginning with z.seek(0)
and use it with the parts of requests
that take file-like objects.
thank you user2357112, but I need to post the data as a bytestring of a zipped file so I'm not sure this solution would work out.
– Tim Achee
17 hours ago
@TimAchee: What makes you think this wouldn't work? It sounds like you're misunderstanding the role of the ZipFile object.
– user2357112
7 hours ago
add a comment |
up vote
0
down vote
zfile
is closed. It's useless to you. The thing you need to use now is z
, the file-like object that was managing the underlying binary storage for the ZipFile.
You can use z.getvalue()
to get a bytestring representing the contents of z
, just like you did with im_out
, or you can seek back to the beginning with z.seek(0)
and use it with the parts of requests
that take file-like objects.
thank you user2357112, but I need to post the data as a bytestring of a zipped file so I'm not sure this solution would work out.
– Tim Achee
17 hours ago
@TimAchee: What makes you think this wouldn't work? It sounds like you're misunderstanding the role of the ZipFile object.
– user2357112
7 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
zfile
is closed. It's useless to you. The thing you need to use now is z
, the file-like object that was managing the underlying binary storage for the ZipFile.
You can use z.getvalue()
to get a bytestring representing the contents of z
, just like you did with im_out
, or you can seek back to the beginning with z.seek(0)
and use it with the parts of requests
that take file-like objects.
zfile
is closed. It's useless to you. The thing you need to use now is z
, the file-like object that was managing the underlying binary storage for the ZipFile.
You can use z.getvalue()
to get a bytestring representing the contents of z
, just like you did with im_out
, or you can seek back to the beginning with z.seek(0)
and use it with the parts of requests
that take file-like objects.
answered 17 hours ago
user2357112
147k12149237
147k12149237
thank you user2357112, but I need to post the data as a bytestring of a zipped file so I'm not sure this solution would work out.
– Tim Achee
17 hours ago
@TimAchee: What makes you think this wouldn't work? It sounds like you're misunderstanding the role of the ZipFile object.
– user2357112
7 hours ago
add a comment |
thank you user2357112, but I need to post the data as a bytestring of a zipped file so I'm not sure this solution would work out.
– Tim Achee
17 hours ago
@TimAchee: What makes you think this wouldn't work? It sounds like you're misunderstanding the role of the ZipFile object.
– user2357112
7 hours ago
thank you user2357112, but I need to post the data as a bytestring of a zipped file so I'm not sure this solution would work out.
– Tim Achee
17 hours ago
thank you user2357112, but I need to post the data as a bytestring of a zipped file so I'm not sure this solution would work out.
– Tim Achee
17 hours ago
@TimAchee: What makes you think this wouldn't work? It sounds like you're misunderstanding the role of the ZipFile object.
– user2357112
7 hours ago
@TimAchee: What makes you think this wouldn't work? It sounds like you're misunderstanding the role of the ZipFile object.
– user2357112
7 hours ago
add a comment |
Tim Achee is a new contributor. Be nice, and check out our Code of Conduct.
Tim Achee is a new contributor. Be nice, and check out our Code of Conduct.
Tim Achee is a new contributor. Be nice, and check out our Code of Conduct.
Tim Achee is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397725%2fpython-load-an-in-memory-zipfile-object-as-bytes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Can you paste your code and error traceback?
– Cheche
yesterday
2
"closed in-memory ZipFile" - you're going to have to explain that some more. Did you wrap a ZipFile around a BytesIO or something?
– user2357112
yesterday
I'm not sure if it helps with an in-memory zip-file (never encountered one in the wild), but you can unzip a single file from an archive: stackoverflow.com/a/46423414/962190
– Arne
yesterday
@user2357112 that's pretty much exactly what I did. I created a ZipFile and used writestr to add a couple of BytesIO to the ZipFile. Then I added the ZipFile as value to a dict, with key as filename, and closed the ZipFile.
– Tim Achee
19 hours ago
@TimAchee: Nope, that still doesn't explain things. What, if anything, did you do to put the ZipFile itself in memory? What arguments did you pass to the ZipFile constructor?
– user2357112
19 hours ago