hi
how to get file from web and save to SPIFFS file system?
how to get file from internet and save to SPIFFS file system?
Re: how to get file from internet and save to SPIFFS file system?
MicroPython doesn't use spiffs. However I keep hearing people refer to it, so would be curious if there's some documentation we need to update or something?
Saving the file to the filesystem is the same as any regular file access in Python, using the standard open/read/write methods. (This isn't specific to MicroPython).
Downloading the file is very simple if your file fits in RAM first - you use urequests to get the data, then you can do whatever you like with it (including saving it to a file).
But if it's a big file, you're going to need to save it chunk at a time.
Saving the file to the filesystem is the same as any regular file access in Python, using the standard open/read/write methods. (This isn't specific to MicroPython).
Downloading the file is very simple if your file fits in RAM first - you use urequests to get the data, then you can do whatever you like with it (including saving it to a file).
But if it's a big file, you're going to need to save it chunk at a time.
Re: how to get file from internet and save to SPIFFS file system?
thank you
I was wrong.
I mean is the micropython file system.
how to use urequests?
please a example.
I was wrong.
I mean is the micropython file system.
how to use urequests?
please a example.
Last edited by AmirJ on Fri Sep 20, 2019 7:28 am, edited 1 time in total.
Re: how to get file from internet and save to SPIFFS file system?
I found these two tutorials with a quick google search: https://techtutorialsx.com/tag/urequests/
They look quite good.
The other thing to remember is that the "urequests" library is based on the Python "requests" library. (This is true of most MicroPython libraries starting with "u", especially the ones in micropython-lib).
They look quite good.
The other thing to remember is that the "urequests" library is based on the Python "requests" library. (This is true of most MicroPython libraries starting with "u", especially the ones in micropython-lib).
Re: how to get file from internet and save to SPIFFS file system?
There's also lots of discussion by people using urequests in this forum.
-
- Posts: 11
- Joined: Wed Feb 23, 2022 5:58 pm
Re: how to get file from internet and save to SPIFFS file system?
I can't seem to get it to work this way, it seems to not have an issue writing the file... maybe... but it won't do anything else with it just keeps giving me the 'could not find file' message if I try to do anything other than create it... which I'm not even sure if it does that since I can do anything else with it to check?jimmo wrote: ↑Thu Sep 19, 2019 11:26 pmMicroPython doesn't use spiffs. However I keep hearing people refer to it, so would be curious if there's some documentation we need to update or something?
Saving the file to the filesystem is the same as any regular file access in Python, using the standard open/read/write methods. (This isn't specific to MicroPython).
Downloading the file is very simple if your file fits in RAM first - you use urequests to get the data, then you can do whatever you like with it (including saving it to a file).
But if it's a big file, you're going to need to save it chunk at a time.
Re: how to get file from internet and save to SPIFFS file system?
at the lowest level, something like this needs to occur:
and use the os module to work withi the filesystem:
it can/should be more complicated than reading the socket directly into the file handle, but at some point, you need to write data into a filehandle that you received from a socket. urequests reads data from the socket into the 'content' property. you can write that directly to the filesystem.
Can you post a reduced example of what you are doing, how saving the data as a file, and how you are trying to access the file?
Code: Select all
with open('/path/to/filename', 'wb') as file_handle: # open as binary since socket produces binary data
file_handle.write(sock.read())
Code: Select all
>>> import os
>>> os.listdir("/")
path
>>> os.listdir("/path/to")
filename
>>> with open('/path/to/filename', 'r') as f: # open for reading as text
print(r.read())
hello!
>>>
Code: Select all
>>> resp = request(method, url, ...)
>>> with open(path_to_file, 'rb') as f:
f.write(resp.content)
>>>