Moving files from flash to sd
Moving files from flash to sd
What are the commands to move files from flash to sd? I have a settings file named 'settings.json' and I am trying to move it from flash to sd. I tried using os.rename('/sd/settings.json','/flash/settings.json'), but I am getting an EPERM error. Additionally, is there a way to do this file management from Windows File Explorer? I don't see a way to access the SD card from there.
Re: Moving files from flash to sd
Since the source and destination are on different file systems, in order to move the file from one place to the other you need to copy it and then remove it from the source.
You can't do this using Windows Explorer since it will only let you see the internal flash or the sd but not both at the same time.
You can do the copy/delete using rshell, or you can write the copy/delete in python and execute it.
Here's the code in rshell which does the copy:
You can't do this using Windows Explorer since it will only let you see the internal flash or the sd but not both at the same time.
You can do the copy/delete using rshell, or you can write the copy/delete in python and execute it.
Here's the code in rshell which does the copy:
Code: Select all
def copy_file(src_filename, dst_filename):
"""Copies a file from one place to another. Both the source and destination
files must exist on the same machine.
"""
try:
with open(src_filename, 'rb') as src_file:
with open(dst_filename, 'wb') as dst_file:
while True:
buf = src_file.read(BUFFER_SIZE)
if len(buf) > 0:
dst_file.write(buf)
if len(buf) < BUFFER_SIZE:
break
return True
except:
return False
Re: Moving files from flash to sd
Thanks @dhylands. You mentioned that you can view either Flash or SD from Windows File Explorer. How do you decide which one you want to view in File Explorer? So far, I have not been able to figure out how to view the contents of the SD card from Windows File Explorer. Thanks.
Re: Moving files from flash to sd
If you have an SD card inserted, then you'll see that in Windows Explorer. If there is no SD card inserted then you'll see the internal flash.
Being able to see the SD card will only work on devices that have native USB support (like the STM32) and when you're plugged into a direct USB port. If you're going through a USB-to-serial converter (which the ESP32 and ESP8266 do) then you won't see the SD card in Windows Explorer.
Being able to see the SD card will only work on devices that have native USB support (like the STM32) and when you're plugged into a direct USB port. If you're going through a USB-to-serial converter (which the ESP32 and ESP8266 do) then you won't see the SD card in Windows Explorer.
Re: Moving files from flash to sd
I do have the SD card inserted. I have confirmed that by being able to call os.chdir('/sd') and then os.listdir() from within the REPL prompt; however, I do not see any of its contents in Windows File Explorer.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Moving files from flash to sd
I'm unclear from the above what hardware you have. Is it a Pyboard 1.x, Pyboard D, ESP32, ESP8266?
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Moving files from flash to sd
I am using the PYBD-SF6W. I can easily see the contents from flash in Windows File Explorer, but I have no way of viewing the contents of the SD card. Within the REPL prompt I have no issues with running os.chdir('/flash') and os.chdir('/sd'), so that I can go back and forth between the two, but only the contents of Flash will show up in Windows File Explorer.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Moving files from flash to sd
I suggest you read this.
tl;dr using Mass Storage mode is a bad idea. I never use it, so the following is guesswork, but I suspect that what you want to do can't easily be done on the Pyboard D because the SD card is disabled until you specifically enable it. By then, MSC mode may have already been assigned to Flash.
There is an easy way. Use Dave Hylands' rshell. The following would move myfile.py:
Moving directories with contents is equally easy with cp -r and rm -r.
Learn rshell and you'll never miss MSC mode. And your filesystem will be safe.
tl;dr using Mass Storage mode is a bad idea. I never use it, so the following is guesswork, but I suspect that what you want to do can't easily be done on the Pyboard D because the SD card is disabled until you specifically enable it. By then, MSC mode may have already been assigned to Flash.
There is an easy way. Use Dave Hylands' rshell. The following would move myfile.py:
Code: Select all
> cp /flash/myfile.py /sd
> rm /flash/myfile.py
Learn rshell and you'll never miss MSC mode. And your filesystem will be safe.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Moving files from flash to sd
Having said that MSC is generally not recomended, in order for MSC to see the sd card, it needs to be mounted in boot.py.
USB (and MSC if enabled) is brought up between boot.py executing and main.py executing. If the sd card isn't mounted when USB is brought up then the internal flash will be what is exposed.
USB (and MSC if enabled) is brought up between boot.py executing and main.py executing. If the sd card isn't mounted when USB is brought up then the internal flash will be what is exposed.