How do you move files inside sdcard with micropython?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: How do you move files inside sdcard with micropython?

Post by Roberthh » Tue Aug 01, 2017 9:50 am

Yes.

f.readlines() would read all lines in the file and put the into a list.
so you could do it like:

Code: Select all

    f = open('/sd/data/data5.csv')
    content = f.readlines()
    f.close()
    for data in content:
        u2.write(data.rstrip("\n"))
or even simpler:

Code: Select all

    f = open('/sd/data/data5.csv')
    for data in f:
        u2.write(data.rstrip("\n"))
    f.close()

skemm
Posts: 10
Joined: Thu Jul 13, 2017 9:31 am

Re: How do you move files inside sdcard with micropython?

Post by skemm » Tue Aug 01, 2017 11:25 am

alright, solved it by implementing a few lines:

lines = f.readlines()
line = lines[iline]
u2.write(line)
if iline < len(lines):
iline = iline + 1
else:
iline = 0

skemm
Posts: 10
Joined: Thu Jul 13, 2017 9:31 am

Re: How do you move files inside sdcard with micropython?

Post by skemm » Wed Aug 02, 2017 3:00 am

Roberthh wrote:Yes.

f.readlines() would read all lines in the file and put the into a list.
so you could do it like:

Code: Select all

    f = open('/sd/data/data5.csv')
    content = f.readlines()
    f.close()
    for data in content:
        u2.write(data.rstrip("\n"))
or even simpler:

Code: Select all

    f = open('/sd/data/data5.csv')
    for data in f:
        u2.write(data.rstrip("\n"))
    f.close()
I tried this but I got memory allocation related errors instead

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

Re: How do you move files inside sdcard with micropython?

Post by Roberthh » Wed Aug 02, 2017 5:28 am

Should not. The memory allocation should be equal or in the second case less. At which line do you get the error?

anhay20171
Posts: 7
Joined: Wed Jul 06, 2022 5:41 am

Re: How do you move files inside sdcard with micropython?

Post by anhay20171 » Wed Jul 06, 2022 5:45 am

Roberthh wrote:
Tue Jul 25, 2017 5:31 am
You can use uos.mkdir(dir) to create a directory, like uos.mkdir("storage"), and use uos.rename() to move files, like:
uos.rename("/SD/data0", "/SD/storage/data0").
Thank you for this answer. I have another question, what if i want to move files inside sd card to "the outside", such as:
uos.rename("sd/a.png","static/a.png")
I have tried the above command but encounter:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 1] EPERM

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: How do you move files inside sdcard with micropython?

Post by jimmo » Wed Jul 06, 2022 6:38 am

anhay20171 wrote:
Wed Jul 06, 2022 5:45 am
Thank you for this answer. I have another question, what if i want to move files inside sd card to "the outside", such as:
uos.rename("sd/a.png","static/a.png")
I have tried the above command but encounter:
This doesn't work in regular Python either when moving across filesystems. You'd normally use functions from the shutil for working with files like this.

There's an implementation of shutil.copyfileobj in micropython-lib

https://github.com/micropython/micropyt ... /shutil.py

You could implement shutil.move as something like the following (untested!)

Code: Select all

def move(src, dest):
  with open(src, 'rb') as src_file:
    with open(dest, 'wb') as dest_file:
      copyfileobj(src_file, dest_file)

anhay20171
Posts: 7
Joined: Wed Jul 06, 2022 5:41 am

Re: How do you move files inside sdcard with micropython?

Post by anhay20171 » Thu Jul 07, 2022 11:08 am

jimmo wrote:
Wed Jul 06, 2022 6:38 am
anhay20171 wrote:
Wed Jul 06, 2022 5:45 am
Thank you for this answer. I have another question, what if i want to move files inside sd card to "the outside", such as:
uos.rename("sd/a.png","static/a.png")
I have tried the above command but encounter:
This doesn't work in regular Python either when moving across filesystems. You'd normally use functions from the shutil for working with files like this.

There's an implementation of shutil.copyfileobj in micropython-lib

https://github.com/micropython/micropyt ... /shutil.py

You could implement shutil.move as something like the following (untested!)

Code: Select all

def move(src, dest):
  with open(src, 'rb') as src_file:
    with open(dest, 'wb') as dest_file:
      copyfileobj(src_file, dest_file)
Thank you! It worked :D

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

Re: How do you move files inside sdcard with micropython?

Post by Roberthh » Thu Jul 07, 2022 11:39 am

the utility upysh from micropython-lib has a cp command. upysh: https://github.com/micropython/micropyt ... thon/upysh

anhay20171
Posts: 7
Joined: Wed Jul 06, 2022 5:41 am

Re: How do you move files inside sdcard with micropython?

Post by anhay20171 » Fri Jul 08, 2022 7:51 am

Roberthh wrote:
Thu Jul 07, 2022 11:39 am
the utility upysh from micropython-lib has a cp command. upysh: https://github.com/micropython/micropyt ... thon/upysh
Thank you! It worked! it's amazing when you and the one above all know solution for this issue. I wonder how have you learned to know the solution, i get no results when searching upysh in micropython doc: https://docs.micropython.org/en/latest/ ... ea=default

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: How do you move files inside sdcard with micropython?

Post by jimmo » Fri Jul 08, 2022 1:09 pm

anhay20171 wrote:
Fri Jul 08, 2022 7:51 am
Thank you! It worked! it's amazing when you and the one above all know solution for this issue. I wonder how have you learned to know the solution, i get no results when searching upysh in micropython doc: https://docs.micropython.org/en/latest/ ... ea=default
It's true, we need to do more to make micropython-lib more discoverable.

There's a note at the end of the first section of https://docs.micropython.org/en/latest/ ... index.html about micropython-lib.

I recommended `shutil` because that is the way to do this because that's how you do it in Python, and honestly I have never even noticed upysh before. I think originally this was designed to provide a sort of shell like interface at the MicroPython REPL. Robert knows about the cp() function because he implemented it :) (see https://github.com/micropython/micropyt ... 413a49ff4b )

I wonder if we should move the implementation of these functions over to shutil and make upysh use shutil?

Post Reply