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: 46
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: 3821
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: 46
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: 3821
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.

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

Re: Moving files from flash to sd

Post by kbrenner » Fri Nov 06, 2020 10:46 pm

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.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Moving files from flash to sd

Post by pythoncoder » Sat Nov 07, 2020 7:21 am

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.

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

Re: Moving files from flash to sd

Post by kbrenner » Thu Nov 12, 2020 7:39 pm

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.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Moving files from flash to sd

Post by pythoncoder » Fri Nov 13, 2020 9:00 am

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:

Code: Select all

> cp /flash/myfile.py /sd
> rm /flash/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.
Peter Hinch
Index to my micropython libraries.

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

Re: Moving files from flash to sd

Post by dhylands » Fri Nov 13, 2020 6:34 pm

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.

Post Reply