file copy .py on board

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
dorad
Posts: 1
Joined: Fri May 14, 2021 2:14 pm

file copy .py on board

Post by dorad » Fri May 14, 2021 2:30 pm

Hello,
i seek a method to copy a file on the board

i find uos.rename() , etc ... but what comand use for copy a file .py ( for exemple blink.py to main.py)

thanks by advance

Roger

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: file copy .py on board

Post by Roberthh » Fri May 14, 2021 7:19 pm

There is no built-in method for that. You have to use a little python script. e.g.

Code: Select all

import os
def copy(s, t):
    try: 
        if os.stat(t)[0] & 0x4000:  # is directory
            t = t.rstrip("/") + "/" + s
    except OSError:
        pass
    with open(s, "rb") as s:
        with open(t, "wb") as t:
            while True:
                l = s.read(512)
                if not l: break
                t.write(l)
And the call:

from <script_name> import copy
copy(source, target)

If target is a directory, the source file will be copied into that directory.

Post Reply