Page 2 of 3

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

Posted: Tue Aug 01, 2017 9:50 am
by Roberthh
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()

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

Posted: Tue Aug 01, 2017 11:25 am
by skemm
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

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

Posted: Wed Aug 02, 2017 3:00 am
by skemm
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

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

Posted: Wed Aug 02, 2017 5:28 am
by Roberthh
Should not. The memory allocation should be equal or in the second case less. At which line do you get the error?

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

Posted: Wed Jul 06, 2022 5:45 am
by anhay20171
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

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

Posted: Wed Jul 06, 2022 6:38 am
by jimmo
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)

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

Posted: Thu Jul 07, 2022 11:08 am
by anhay20171
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

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

Posted: Thu Jul 07, 2022 11:39 am
by Roberthh
the utility upysh from micropython-lib has a cp command. upysh: https://github.com/micropython/micropyt ... thon/upysh

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

Posted: Fri Jul 08, 2022 7:51 am
by anhay20171
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

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

Posted: Fri Jul 08, 2022 1:09 pm
by jimmo
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?