I am uploading zip file using file loader and want to split that zip file into 3 folder but my splitting code take string inputs it not reading media object i.e. my zip file so is there possible that we read that zip file as string so that my splitting code able to read it and split it.
Hello, please share code of what you have tried.
If this is a pure Python question, you may also search Stack Overflow as well.
Have you seen these docs on Anvil Media? You might be able to use get_bytes()
.
yes i have seen its but still it not working can you guide me by provide any piece of code
I was guiding you to get_bytes()
. Did you try that? If so, please let us know what happened.
PYTHON CODE
import anvil.media
import splitfolders
from zipfile import ZipFile
@anvil.server.callable
def split(zip_file, output,train_ratio, test_ratio,validate_ratio):
zip_file = zip_file.open(io.BytesIO(files.get_bytes()))
splitfolders.ratio( zip_file, output , seed=42, ratio=(float(train_ratio), float(test_ratio),float(validate_ratio)))
#return(splitfolders)
AND ERROR ON ANVIL
AttributeError: 'StreamingMedia' object has no attribute 'open'
at C:/ProgramData/Anaconda3/lib/site-packages/anvil/__init__.py, line 48
called from <ipython-input-2-1f582dae3287>, line 9
called from [Form1, line 21](javascript:void(0))
Just a general tip, but you can format your code so that it is easier to read on the forum by wrapping it in backticks? Like so,
```python
print('this will be syntax highlighted')
```
It is not totally clear to me which line the error occurs on. Could you let us know? I assume it is on:
zip_file = zip_file.open(io.BytesIO(files.get_bytes()))
yes in this line
and this is error
Yeah, that makes sense. Your variable zip_file
doesn’t have .open()
as an attribute.
What is the type of the variable zip_file
? That is, what does print(type(zip_file)
show? I ask because if it is a Media object, the docs show examples of how to get the bytes (e.g., zip_file.get_bytes()
).
Or maybe the folder needs to be unzipped first into a regular folder as was shown in other recent posts. That probably makes the most sense. I’ve never used splitfolders
before, but it seems to take a folder as its input.
Sorry I can’t be of more help!
no problem thanks alot!!
also worth checking your call signature here and variable names - what is files
? it doesn’t seem to exist!
as @alcampopiano rightly infers zip_file
is a Media object.
zip_file = io.BytesIO(zipfile.get_bytes())
is probably closer to what you want
Actually I just want to split my image dataset into train, test and validate folder and I want to take input folder containing my class folders which contains images but anvil not provide facility to upload whole folder using file loader it just able to upload one file at a time and when I take input path of folder its only working for me but not for my friend coz she has no access of folder and she got error thats why I try zip file but it is very challenging for me and i also try zip_file = io.BytesIO(zipfile.get_bytes()) but again i got an error.
Now I got this error
Can you post the error and the context. Ie the line that the error is referring to.
I don’t see you trying to access seek
in the code snippets you posted.
Very unclear what this should mean:
However, not understanding what that means, my 2 cents on a possibly different approach.
@fatimamahmood717 in order to avoid to fight with all that file-like memory-stream code, why not just save the zip file in Anvil’s-server-environment /tmp folder, use it, then delete it?
Have you checked this secion of the docs where it’s explained how save a MediaObject to disk?
def write_a_media_object():
media_object = anvil.BlobMedia('text/plain', b'Hello, world', name='hello_world.txt')
with open('/tmp/hello_world.txt', 'wb+') as f:
# Write the byte contents of the media object to 'tmp/my-file.txt'
# (we opened the file in binary mode, so f.write() accepts bytes)
f.write(media_object.get_bytes())
This way may be you find yourself in a more familiar local-pc-like environment and managing a filesystem-hosted zip file might result more straightforward.
Just keep in mind this:
Read and write files as normal
You can also use Python’s open
to read and write files as normal. Your filesystem is your own; other users do not have access to it.
Files in your filesystem are temporary, and may be removed without warning. We advise you only to access files in the /tmp
directory.
BR