Moving files from flash to sd

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
kbrenner
Posts: 13
Joined: Mon Jan 20, 2020 8:05 pm

Moving files from flash to sd

Post by kbrenner » Tue Sep 15, 2020 5:44 pm

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.

User avatar
dhylands
Posts: 3465
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Moving files from flash to sd

Post by dhylands » Tue Sep 15, 2020 7:22 pm

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:

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

kbrenner
Posts: 13
Joined: Mon Jan 20, 2020 8:05 pm

Re: Moving files from flash to sd

Post by kbrenner » Wed Sep 30, 2020 9:01 pm

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.

User avatar
dhylands
Posts: 3465
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Moving files from flash to sd

Post by dhylands » Wed Sep 30, 2020 9:14 pm

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.

Post Reply