Page 1 of 1

file copy .py on board

Posted: Fri May 14, 2021 2:30 pm
by dorad
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

Re: file copy .py on board

Posted: Fri May 14, 2021 7:19 pm
by Roberthh
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.